Robert  Kodra Robert Kodra -4 years ago 185 Question

Copy file to remote computer

I have created a program which will copy from my computer to other remote computers a specific file. I have successfully did this, but only in the driver C:\ . My question is how can I copy the file to the D:\ drive?

I have tried adding \\D:\testnew\right.bmp but with no luck. Let me know!

Imports System
Imports System.Runtime.InteropServices
Imports System.Security.Principal
Imports System.Security.Permissions
Public Class Form1
<DllImport("advapi32.DLL", SetLastError:=True)> _
Public Shared Function LogonUser(ByVal lpszUsername As String, ByVal lpszDomain As String, _
ByVal lpszPassword As String, ByVal dwLogonType As Integer, ByVal dwLogonProvider As Integer, _
ByRef phToken As IntPtr) As Integer
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim admin_token As IntPtr
Dim wid_current As WindowsIdentity = WindowsIdentity.GetCurrent()
Dim wid_admin As WindowsIdentity = Nothing
Dim wic As WindowsImpersonationContext = Nothing
MessageBox.Show("Copying file...")
If LogonUser("Local Admin name", "Local computer name", "pwd", 9, 0, admin_token) <> 0 Then
wid_admin = New WindowsIdentity(admin_token)
wic = wid_admin.Impersonate()
System.IO.File.Copy("C:\right.bmp", "\\\testnew\right.bmp", True)
MessageBox.Show("Copy succeeded")
MessageBox.Show("Copy Failed")
End If
Catch se As System.Exception
Dim ret As Integer = Marshal.GetLastWin32Error()
MessageBox.Show(ret.ToString(), "Error code: " + ret.ToString())
If wic IsNot Nothing Then
End If
End Try
End Sub
End Class

Answer Source

the path you're looking for is:


if you are required to interact with the remote machine regularly it would be wise to map the network path as a network drive in your machine.

mapping network drive

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download