我尝试使用Microsoft C#中的示例。 The link is here.
但是我现在得到了标题中的错误(认为它从丹麦语翻译成英语)
它说有问题的路线是:
Stream ftpstream = ftp.GetRequestStream();
(当然在真实的代码中我的实际地址和传递。)
(我也尝试创建一个文件夹,但它有效)
整个功能是:
public static void MakeFTPFile(string FileName,string[] Lines)
{
string sourcefilepath = "";
string ftpurl = "ftp://ftpAdress";
string ftpusername = "Username";
string ftppassword = "Pass";
FileName += ".txt";
if (!File.Exists(FileName))
{
// Create a file to write to.
using (StreamWriter sw = File.CreateText(FileName))
{
for (int i = 0; i < Lines.Length; i++)
{
sw.WriteLine(Lines[i]);
}
}
}
string filename = Path.GetFileName(FileName);
string ftpfullpath = ftpurl;
FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);
ftp.Credentials = new NetworkCredential(ftpusername, ftppassword);
ftp.KeepAlive = true;
ftp.UseBinary = true;
ftp.Method = WebRequestMethods.Ftp.UploadFile;
FileStream fs = File.OpenRead(FileName);
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();
}