简体   繁体   中英

File.Copy with urls c#

It´s possible to use two urls with File.Copy with C#? I´m getting different errors:

  1. URI formats are not supported

  2. The given path's format is not supported.

There is a question some similar but is not answered.

I want copy from a directory that is in server1 to another server and the urls are http

Thanks

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:\testfile.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#

Look here: http://msdn.microsoft.com/en-us/library/system.io.file.copy.aspx

If you mean http urls it is not possible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM