带有网址的File.Copy c#

JMG*_*JMG 1 c# copy c#-4.0

使用带C#的File.Copy可以使用两个URL吗?我得到了不同的错误:

  1. 不支持URI格式

  2. 不支持给定路径的格式.

有一个类似的问题,但没有回答.

我希望从server1中的目录复制到另一个服务器,并且URL是http

谢谢

Mas*_*uso 9

只有在我们不讨论FTP的情况下才能使用File.Copy.在这种情况下,您可以使用下面的代码

如果您有FTP,可以使用以下代码:

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();  
}
Run Code Online (Sandbox Code Playgroud)

那么你可以做到

ftpfile(@"/testfolder/testfile.xml", @"c:\testfile.xml");
Run Code Online (Sandbox Code Playgroud)

如果我们在同一网络上讨论共享文件夹,您可以执行以下操作:

File.Copy(filepath, "\\\\192.168.1.28\\Files");
Run Code Online (Sandbox Code Playgroud)

对于HTTP,您可以使用以下内容:

using(WebClient client = new WebClient()) {
    client.UploadFile(address, filePath);
}
Run Code Online (Sandbox Code Playgroud)

资源:

使用C#通过HTTP POST发送文件