关于FTP指定目录的问题(C#)

.Net技术 码拜 9年前 (2015-05-10) 2874次浏览 0个评论

我用C#访问ftp的时候,他有一个默认路径,我现在要做的是访问他的根目录下的其他目录,例如下面:进入
ftp://123.12.12.12 进来后他默认的路径是ftp://123.12.12.12/aaa/bbb
我要访问ftp://123.12.12.12/ccc/ddd这个目录,怎么用程序控制,让我可以切换目录,在网上看到一些ftp的操作类,没有我需要的,求各位大神们给个方法啊,急急急急!!!

10分

你直接把
ftp = (FtpWebRequest)FtpWebRequest.Create(new Uri(“ftp://123.12.12.12/aaa/bbb”));

10分
private void DownloadImages(string localFolder, string remotPath, string fileName)
        {
            FtpWebRequest reqFTP;

            string ftpServerIP = ConfigurationManager.AppSettings[“FtpServer”];
            string ftpUserID = ConfigurationManager.AppSettings[“FtpUid”];
            string ftpPassword = ConfigurationManager.AppSettings[“FtpPwd”];
            string ftpPort = ConfigurationManager.AppSettings[“FtpPort”];

            try
            {
                FileStream outputStream = new FileStream(localFolder + “\” + fileName, FileMode.Create);

                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(“ftp://” + ftpServerIP + “:” + ftpPort + “/” + remotPath + “/” + fileName));

                reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;

                reqFTP.UseBinary = true;

                reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);

                FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();

                Stream ftpStream = response.GetResponseStream();

                long cl = response.ContentLength;

                int bufferSize = 2048;

                int readCount;

                byte[] buffer = new byte[bufferSize];

                readCount = ftpStream.Read(buffer, 0, bufferSize);

                while (readCount > 0)
                {
                    outputStream.Write(buffer, 0, readCount);

                    readCount = ftpStream.Read(buffer, 0, bufferSize);
                }

                ftpStream.Close();

                outputStream.Close();

                response.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

//ftpServerIP 、ftpUserID 、ftpPassword、ftpPort  这些配置都写在App.config文件里
<?xml version=”1.0″ encoding=”utf-8″ ?>
<configuration>
  <appSettings>
    <add key=”FtpServer” value=”192.168.1.11″/>
    <add key=”FtpUid” value=”orms”/>
    <add key=”FtpPwd” value=”orms”/>
    <add key=”FtpPort” value=”22″/>
  </appSettings>
</configuration>

调用方法示例:

DownloadImages(@”d:\temp”, “ccc/ddd”, “test.jpg”);

引用

http://www.cnblogs.com/swtseaman/archive/2011/03/29/1998611.html

里面有一个GotoDirectory()方法,直接调用
GotoDirectory(“aaa/bbb”,false)

好吧 各位 我自己也弄出来了,刚才debug的时候才弄明白的,解决的方法和你们说的差不多,谢谢啊
楼主后来怎么解决的?

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明关于FTP指定目录的问题(C#)
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!