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 ?
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 ?
If these are two servers on the same local network and you have appropriate access permissions, you can probably get by using the File.Copy
method, 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, the File.Copy
method 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 WNetUseConnection
to 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.
You probably want to use FTP or SSH as a transport method to do something like this. You just have to set up the right services on the server side to use these communication methods.
Then you need a .Net library that speaks these protocols. Here is one:
http://www.weonlydo.com/FtpDLX.NET/ftp-sftp-ssl-net.asp
It works, but I wasn't all that impressed by it for various reasons. I'm sure there are more out there.
And, yes, each of those protocols will challenge the connecting system for a username/password.
I would add an answer for the part "password identification on from and to" and if you have the admin permissions:
use the administrator shares like this:
\\server\c$