File.Copy with urls c# [英] File.Copy with urls c#

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

问题描述

是否可以将两个 url 与 File.Copy 与 C# 一起使用?我收到了不同的错误:

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.

我想从 server1 中的目录复制到另一台服务器,网址是 http

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

谢谢

推荐答案

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

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();  
}

然后你就可以了

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

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

for HTTP you can use the below:

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

来源:

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

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

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