Question copying one file to another system

joemack95

Member
Joined
Nov 21, 2016
Messages
5
Programming Experience
Beginner
So I need help copying one file to another system on a different network heres my code so far
        Try


            Dim fso, WshNetwork
            Const OverwriteExisting = True
            WshNetwork = CreateObject("Wscript.Network")
            fso = CreateObject("Scripting.FileSystemObject")


            WshNetwork.mapnetworkdrive("x:", "\\10.72.14.14\web$", , "monitoring\caretaker", "caretaker")


            fso.CopyFile("C:\Users\Joe\Documents\From\joeTest.txt", "\\10.72.14.14\Web\joeTest.txt", OverwriteExisting)
            MessageBox.Show("Transfer Complete")


        Catch ex As Exception
            MessageBox.Show("error ..." & ex.Message, "error")
        End Try

I keep getting error messages like:hresult 0x800a004c (ctl_e_pathnotfound) and network name not found

if anyone could help thatd be great
 
I'm a little confused with your code, first you're mapping to a network drive: "\\10.72.14.14\web$" as "X:" then trying to copy a file to "\\10.72.14.14\web", all while using outdated vb6/vbs com objects.
So which path is it failing to find, "\\10.72.14.14\web$"? Or "\\10.72.14.14\web"?
My guess would be the latter, the one without the $.

In any event, you can use Net.exe to map the network drive as if that process ever changes in Windows you don't need to worry about updating a dll in your app. Also you can use System.IO.File.Copy to actually copy the file.

Here's an example, though I haven't tested this as I didn't have the time to do that yet:
Imports System.IO

Public Class Form1

    Private Function CopyFile(SourceFile As String, DestinationFolderPath As String) As Boolean
        Try
            'Make sure the source file exists
            If File.Exists(SourceFile) Then

                'Check if the destination directory exists
                If Not Directory.Exists(DestinationFolderPath) Then
                    'Map the network drive
                    MapDrive(DestinationFolderPath.Split(":"c)(0I), "\\10.72.14.14\web$", "monitoring\caretaker", "caretaker")
                End If

                'Copy the file
                File.Copy(SourceFile, Path.Combine(DestinationFolderPath, Path.GetFileName(SourceFile)), True)

                'All is good
                Return True
            End If
        Catch ex As Exception
            'Display error
            MessageBox.Show(ex.ToString(), "CopyFile Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

        'Got an error
        Return False
    End Function

    Private Sub MapDrive(DriveLetter As String, UNCPath As String, strUsername As String, strPassword As String)
        Dim si As New ProcessStartInfo()

        'Info for mapping the drive
        With si
            .FileName = "net.exe"
            .CreateNoWindow = True
            .Arguments = $" Use {DriveLetter}:{UNCPath} {strPassword} /USER:{strUsername}"
        End With

        'Map the network drive
        Using p As Process = Process.Start(si)
            p.WaitForExit()
        End Using
    End Sub

End Class
Call the function using:
If CopyFile("C:\Users\Joe\Documents\From\joeTest.txt", "X:") Then Messagebox.Show("Success")
 
Back
Top