苍蓝残影 2014-01-22 07:51 采纳率: 0%
浏览 3664

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条回答

  • 花猫阳 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;
        }   
    
    评论

报告相同问题?

悬赏问题

  • ¥15 用三极管设计—个共射极放大电路
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示