C# Tutorials and offshore development in India
Tutorials Resources Forum Reviews Communities Interview Jobs Projects Training Videos


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...


Birthday Greetings
Learn Windows 7: Minesweeper - The intelligent game that comes with Windows Operating system   Computers who are used to playing the offline games available with Windows OS will be well aware about the minesweeper game. There are many users who are least aware of how to play this game. This is really a very addictive game once you know how to play it. In this resource I will make you all addictive to this game as I am.



Resources » Code Snippets » File Operations »

Copy files from remote machine


Posted Date: 11 Jun 2009    Resource Type: Code Snippets    Category: File Operations
Author: Viji RAJKUMARMember Level: Diamond    
Rating: 1 out of 5Points: 10



To copy files from local machine we can simply do

System.IO.File.Copy()

as we are already logged in to the machine.

How to copy the files from a remote machine where we have not logged in?

Here is the solution.

We need to provide domainname, user name and password of the remote machine.



Imports System.Security.Principal
Imports System.Runtime.InteropServices

Public Class Form1
Inherits System.Windows.Forms.Form

'Functions needed to copy files from remote machine

_
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


Public Sub copyRemoteFiles(ByVal sourceFile As String, ByVal destFile As String)
Dim admin_token As IntPtr
Dim wid_current As WindowsIdentity = WindowsIdentity.GetCurrent()
Dim wid_admin As WindowsIdentity = Nothing
Dim wic As WindowsImpersonationContext = Nothing

Try
If LogonUser(sUserName, sDomainName, sPassword, 9, 0, admin_token) <> 0 Then
wid_admin = New WindowsIdentity(admin_token)
wic = wid_admin.Impersonate()
If System.IO.File.Exists(sourceFile) Then
System.IO.File.Copy(sourceFile, destFile, True)
Else 'Copy Failed
Exit Sub
End If
Else
Exit Sub
End If
Catch se As System.Exception
Dim ret As Integer = Marshal.GetLastWin32Error()
MessageBox.Show(ret.ToString(), "Error code: " + ret.ToString())
MessageBox.Show(se.Message)
If wic IsNot Nothing Then
wic.Undo()
End If
Exit Sub
Finally
If wic IsNot Nothing Then
wic.Undo()
End If
End Try

End Sub


'Copy remote file
Private Sub btnCopyFiles_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCopyFiles.Click

Dim sourceFile As String
Dim destFile as String

sourceFile = txtSourceFile.Text.Trim 'Remote File Path
destFile = txtDestFile.Text.Trim 'Local File Path


'Copy remote file to local file
copyRemoteFiles(sourceFile, destFile)

End Sub

End Class






Responses to the resource: "Copy files from remote machine"

No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Copy files from remote machine  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Delete the file using C#.net
Previous Resource: File upload
Return to Resources
Post New Resource
Category: File Operations


Post resources and earn money!
 
More Resources



About Us    Contact Us    Privacy Policy    Terms Of Use