File.Copy使用URL C# [英] File.Copy with urls c#

查看:340
本文介绍了File.Copy使用URL C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能汇入作业与File.Copy使用两个URL与C#? I'm得到不同的错误:

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


  1. URI格式不被支持

  1. URI formats are not supported

指定路径的格式不支持。

The given path's format is not supported.

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

There is a question some similar but is not answered.

我想从一个目录在服务器1到另一台服务器复制网址为http

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

谢谢

推荐答案

您可以使用File.Copy只有当我们不是在谈论一个FTP。在这种情况下,你可以使用下面

you can use File.Copy only if we are not talking about an FTP. in that case you can use the code below

如果你有一个FTP可以使用下面的代码的代码:

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");

有关HTTP可以使用如下:

for HTTP you can use the below:

using(WebClient client = new WebClient()) {
    client.UploadFile(address, filePath);
}

来源:

通过HTTP POST发送文件用C#

这篇关于File.Copy使用URL C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆