可能重复:
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();
}
我有上面的代码将特定文件上传到路径。
在ftp内部,如何设置上传文件的特定目录?
在这种情况下,我上传到具有嵌入式Linux的机器,因此需要在/ root / somedirectory下
编辑:我已经尝试了在请求路径中实际包含目录路径的建议,但它只是抛出了一个System.Net.WebException,并在request.GetRequestStream();
答案 0 :(得分:2)
您将目录放在请求的路径中,即
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://"
+ ddcdao.ddcAddress + "/" + someDirectory
+ "/" someFile);