Results 1 to 2 of 2

Thread: copying one file to another system

  1. #1
    joemack95 is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.5
    Join Date
    Nov 2016

    copying one file to another system

    So I need help copying one file to another system on a different network heres my code so far


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

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

    fso.CopyFile("C:\Users\Joe\Documents\From\joeTest.txt", "\\\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

  2. #2
    JuggaloBrotha's Avatar
    JuggaloBrotha is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Jun 2004
    Lansing, MI; USA
    I'm a little confused with your code, first you're mapping to a network drive: "\\\web$" as "X:" then trying to copy a file to "\\\web", all while using outdated vb6/vbs com objects.
    So which path is it failing to find, "\\\web$"? Or "\\\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
    '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), "\\\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)
    End Using
    End Sub

    End Class
    Call the function using:
    If CopyFile("C:\Users\Joe\Documents\From\joeTest.txt", "X:") Then Messagebox.Show("Success")
    Currently using: VS 2010 Ultimate on Win7 Ultimate x64.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts