wpf 更改FTP上传文件的路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12292830/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing path to upload file in FTP
提问by l46kok
Possible Duplicate:
How do you change directories using FtpWebRequest (.NET)?
private void InitFTPTransfer(string filePath)
{
string[] ftpAddress = ddcdao.ddcAddress.Split(new string[] { "http://" }, StringSplitOptions.None);
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://" + ftpAddress[1] + "/root/" + Path.GetFileName(filePath));
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(Properties.Settings.Default.SysFTPID, Properties.Settings.Default.SysFTPPassword);
byte[] fileContents = File.ReadAllBytes(filePath);
request.ContentLength = fileContents.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
response.Close();
}
I have the above code to upload a particular file to a path.
我有上面的代码将特定文件上传到路径。
Inside the ftp, how do I set a particular directory to upload the file on?
在 ftp 中,如何设置特定目录来上传文件?
In this case, I'm uploading to a machine with embedded linux so it needs to be under /root/somedirectory
在这种情况下,我上传到一台带有嵌入式 linux 的机器,所以它需要在 /root/somedirectory 下
Edit: I've tried the suggestions of actually including the directory path in the request path but it just throws a System.Net.WebException with the message "System error" on the line of request.GetRequestStream();
编辑:我已经尝试了在请求路径中实际包含目录路径的建议,但它只是在 request.GetRequestStream(); 行上抛出一个带有“系统错误”消息的 System.Net.WebException;
回答by StuartLC
You put the directory in the request's path, i.e.
你把目录放在请求的路径中,即
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://"
+ ddcdao.ddcAddress + "/" + someDirectory
+ "/" someFile);

