gpt4 book ai didi

c# - File.Copy with urls c#

转载 作者:太空宇宙 更新时间:2023-11-03 18:15:33 26 4
gpt4 key购买 nike

C# 中的 File.Copy 可以使用两个 url 吗?我收到不同的错误:

  1. 不支持 URI 格式

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

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

我想从 server1 中的目录复制到另一台服务器并且 url 是 http

谢谢

最佳答案

只有当我们不谈论 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();
}

然后你可以做

ftpfile(@"/testfolder/testfile.xml", @"c:\testfile.xml");

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

File.Copy(filepath, "\\\\192.168.1.28\\Files");

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

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

来源:

Send a file via HTTP POST with C#

关于c# - File.Copy with urls c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7009462/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com