I have to copy some file from server to another, I thought to use File.Copy but how I specify there the location of a file ? and also there can be a password identification on from and to.
Any ideas ?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If these are two servers on the same local network and you have appropriate access permissions, you can probably get by using the
File.Copymethod, and passing whatever path you would normally use to access the network share in Windows Explorer (this is probably a UNC path, like\\TheRemoteServer\SharedFolder\MyFiles). If you can copy the file in Explorer, theFile.Copymethod will more than likely succeed also.However, if you need to authenticate as a different user on the remote server to have the appropriate permissions to copy files, things get a little more complicated because there is no easy .NET API for this. You might look into this answer to another question. Essentially, it p/invokes to log on to the remote machine as a different user. Once you’re logged on, you can perform the file copy normally.
Also see this question: Accessing a Shared File (UNC) From a Remote, Non-Trusted Domain With Credentials—chiefly the answer that suggests using
WNetUseConnectionto connect to a UNC path on a remote machine with authentication.Finally, just as a precaution, I feel like I must point out that whenever you start doing network file copies, you need to be very careful to check the availability of the network path before you just blindly start copying (the remote server could be down, the local machine could have lost its connection to the network, etc.). You might also need to consider handling cases where the network operation times out.