我一直在尝试将多个xml文件上传到服务器。但是当我收到此错误时,找不到目录,我仍然可以在同一目录中看到我的文件。
这是我的代码:
String ftpurl = "100.100.0.35/Vin"; // e.g. fake IDs
String ftpusername = "ftp"; // e.g. fake username
String ftppassword = "Password"; // e.g. fake password
string path = @"C:\\Users\\Desktop\\LUVS\\";
// string[] filePaths = Directory.GetFiles(sourcefilepath, "*.xml", SearchOption.AllDirectories);
// string[] filePaths = Directory.GetFiles(@"C:\\Users\Desktop\\LUVS/", "*.xml", SearchOption.AllDirectories);
string[] filePaths = Directory.GetFiles(path, @"*.xml", SearchOption.AllDirectories);
// Work with each file individually
foreach (var files in filePaths)
{
string filename = Path.GetFileName(path);
string ftpfullpath = "ftp://" + ftpurl + "/" + filename;
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(path);
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();
}
答案 0 :(得分:0)
你在foreach循环中犯了错误,用以下
替换循环 foreach (var file in filePaths)
{
string filename = Path.GetFileName(file);
string ftpfullpath = "ftp://" + ftpurl + "/" + filename;
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(file);
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();
}