在C#中使用FTP发送文件文件、FTP

2023-09-04 00:09:28 作者:青橘酸心

我试图发送使用ftp文件。我有以下的code:

I'm trying to send a file using ftp. I have the following code:

string server = "x.x.x.x";  // Just the IP Address 

FileStream stream = File.OpenRead(filename);
byte[] buffer = new byte[stream.Length];

WebRequest request = WebRequest.Create("ftp://" + server);
request.Method = WebRequestMethods.Ftp.UploadFile;            
request.Credentials = new NetworkCredential(username, password);

Stream reqStream = request.GetRequestStream(); // This line fails
reqStream.Write(buffer, 0, buffer.Length);
reqStream.Close();

但是,当我运行它,我得到以下错误:

But when I run it, I get the following error:

请求的URI是该FTP命令无效。

The requested URI is invalid for this FTP command.

请谁能告诉我为什么吗?我使用这个错误?

Please can anyone tell me why? Am I using this incorrectly?

推荐答案

我想你需要指定的路径和文件名,你要上传过,所以我想应该是的:

I think you need to specify the path and filename you're uploading too, so I think it should be either of:

WebRequest request = WebRequest.Create("ftp://" + server + "/");

WebRequest request = WebRequest.Create("ftp://" + server + "/filename.ext");