cuin123
苍蓝残影
采纳率0%
2014-01-22 07:51 阅读 3.4k

C# 如何获取FTP夹内的文件路径

请问在winform里怎样才能连接到FTP,并获取文件路径。例如:ftp地址是:ftp://192.168.1.1/ 用户名是admin 密码是111 。登录FTP后,我选择了一个文件:picture.jpg 请问我怎样才能打开FTP目录,并且获得我鼠标选取的文件的FTP路径:ftp://192.168.1.1/picture.jpg ,感谢您的解答。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • u011762977 花猫阳 2014-02-15 03:55

    你是想通过FTP地址获取到FTP部署的本地路径,如把ftp://192.168.1.1/picture.jpg换成D:\FTP\picture.jpg,这个受FTP安全限制是无法获取的,FTP部署在服务器上面,就算获取到了实际路径你也没法操作,部署在本地这样获取就没什么意义了。
    下面提供FTP的上传和下载的两个方法,供参考:
    private bool UploadFileByFtpWebRequest(string fileName, string ftpServerIP, string path, string userName, string password)
    {
    bool result = true;
    string url = string.Format("ftp://{0}:{1}/{2}", ftpServerIP, path, fileName.Substring(fileName.LastIndexOf(@"\") + 1));

            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(url);
            request.UseBinary = true;
            request.Credentials = new NetworkCredential(userName, password);
            request.Method = WebRequestMethods.Ftp.UploadFile;
            Stream stream = request.GetRequestStream();
            FileStream fileStream = new FileStream(fileName, FileMode.Open);
            int packageSize = 1024 * 1024;
            int packageCount = (int)(fileStream.Length / packageSize);
            int rest = (int)(fileStream.Length % packageSize);
    
            for (int index = 0; index < packageCount; index++)
            {
                byte[] buffer = new byte[packageSize];
                fileStream.Read(buffer, 0, buffer.Length);
                stream.Write(buffer, 0, buffer.Length);
                this.pgbShow.Value = buffer.Length / (int)fileStream.Length * 100;
                //this.lblLeng.Text = pgbShow.Value.ToString();
                lblLeng.Invoke(new myDelegate(delegate() 
                    {
                        this.lblLeng.Text = pgbShow.Value.ToString() + "%";
                    }));
    
    
            }
            if (rest != 0)
            {
                byte[] buffer = new byte[rest];
                fileStream.Read(buffer, 0, buffer.Length);
                stream.Write(buffer, 0, buffer.Length);
                this.BeginInvoke(new DoSomething(delegate()
                {
                    //if (rest != 0) this.pgbShow.Maximum += 1;
                    this.pgbShow.Value = 100;
                }));
            }
            stream.Close();
            fileStream.Close();
            this.BeginInvoke(new DoSomething(delegate()
            {
                //this.pgbShow.Value = 0;
            }));
            return result;
        }
    
        public string Download(string userId, string pwd, string ftpPath, string filePath, string fileName)
        {
            string sRet = "下载成功!";
            FtpWebRequest reqFTP;
            try
            {
                FileStream outputStream = new FileStream(filePath + fileName, FileMode.Create);
    
                // 根据uri创建FtpWebRequest对象     
                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpPath + fileName));
    
                // 指定执行什么命令    
                reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
    
                // 指定数据传输类型    
                reqFTP.UseBinary = true;
                reqFTP.UsePassive = false;
    
                // ftp用户名和密码    
                reqFTP.Credentials = new NetworkCredential(userId, pwd);
    
                FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
    
                // 把下载的文件写入流  
                Stream ftpStream = response.GetResponseStream();
    
                long cl = response.ContentLength;
    
                // 缓冲大小设置为2kb    
                int bufferSize = 2048;
                int readCount;
                byte[] buffer = new byte[bufferSize];
    
                // 每次读文件流的2kb    
                readCount = ftpStream.Read(buffer, 0, bufferSize);
                while (readCount > 0)
                {
                    // 把内容从文件流写入     
                    outputStream.Write(buffer, 0, readCount);
                    readCount = ftpStream.Read(buffer, 0, bufferSize);
                }
    
                //关闭两个流和ftp连接  
                ftpStream.Close();
                outputStream.Close();
                response.Close();
    
    
            }
            catch (Exception ex)
            {
                sRet = ex.Message;
            }
    
            //返回下载结果(是否下载成功)  
            return sRet;
        }   
    
    点赞 评论 复制链接分享