you can use File.Copy only if we are not talking about an FTP. in that case you can use the code below
if you have an FTP you can use the below code:
public void ftpfile(string ftpfilepath, string inputfilepath)
{
string ftphost = "127.0.0.1";
//here correct hostname or IP of the ftp server to be given
string ftpfullpath = "ftp://" + ftphost + ftpfilepath;
FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);
ftp.Credentials = new NetworkCredential("userid", "password");
//userid and password for the ftp server to given
ftp.KeepAlive = true;
ftp.UseBinary = true;
ftp.Method = WebRequestMethods.Ftp.UploadFile;
FileStream fs = File.OpenRead(inputfilepath);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
fs.Close();
Stream ftpstream = ftp.GetRequestStream();
ftpstream.Write(buffer, 0, buffer.Length);
ftpstream.Close();
}
then you can do
ftpfile(@"/testfolder/testfile.xml", @"c:estfile.xml");
if we are talking about a shared folder on the same network you can do the below:
File.Copy(filepath, "\\192.168.1.28\Files");
for HTTP you can use the below:
using(WebClient client = new WebClient()) {
client.UploadFile(address, filePath);
}
source:
Send a file via HTTP POST with C#
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…