当前位置: 代码迷 >> C# >> FTP下载文件有关问题,
  详细解决方案

FTP下载文件有关问题,

热度:56   发布时间:2016-05-05 04:14:56.0
FTP下载文件问题,急!!!!
       
private void button3_Click(object sender, EventArgs e)
        {
            string KSSJ = dtpKSSJ.Value.ToString("yyyy-MM-dd");
            string SavePath = this.textBox1.Text;
            string ftpUrl = "";
            if (this.textBox1.Text.Trim() != "")
            {
                if (dataGridView1.Rows.Count > 0)
                {
                    for (int i = 0; i < dataGridView1.Rows.Count; i++)
                    {
                        if (bool.Parse(dataGridView1.Rows[i].Cells[9].Value.ToString()) == false)
                        {
                            string FileName = this.dataGridView1.Rows[i].Cells[0].Value.ToString();//获取FTP文件名
                            string FilePath1 = this.dataGridView1.Rows[i].Cells[4].Value.ToString();//获取FTP文件路径
                            string FilePath2 = FilePath1.Substring(0, FilePath1.Length - 1);
                            ftpUrl = FilePath2.Substring(1);
                            string Model_No = this.dataGridView1.Rows[i].Cells[5].Value.ToString();//获取型号
                            string Type_Work = this.dataGridView1.Rows[i].Cells[6].Value.ToString();//获取工种
                            int FileSize = Convert.ToInt32(this.dataGridView1.Rows[i].Cells[2].Value.ToString());
                            string path = string.Format(@"{0}\{1}\{2}\{3}\", SavePath, KSSJ, Model_No, Type_Work);
                            string url = "ftp://15.5.240.15/" + ftpUrl;
                            // DownLoad(FileName, ftpUrl, SavePath, KSSJ, Model_No, Type_Work);
                            Download(FileName, url, path,FileSize);
                            //MessageBox.Show("可以保存!");


                        }
                    }
                }
                else
                {
                    MessageBox.Show("没有指定数据!");
                }
            }
            else
            {
                MessageBox.Show("请指定存放目录!", "提示");
            }
        }

        /// <summary>
        /// 下载
        /// </summary>
        /// <param name="FileName"></param>
        /// <param name="ftpUrl"></param>
        /// <param name="path"></param>
        public void Download(string FileName, string ftpUrl, string path,int FileSize)
        {
            FtpWebRequest reqFTP;
            try
            {
                FileStream outputStream = new FileStream(path + "\\" + FileName, FileMode.Create);
                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpUrl + FileName));
                reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
                reqFTP.KeepAlive = false;
                reqFTP.UseBinary = true;
                reqFTP.Credentials = new NetworkCredential("bft1", "Aa149608");
                FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
                Stream ftpStream = response.GetResponseStream();
                long cl = response.ContentLength;
                int bufferSize = FileSize;
                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);
            }
        }

麻烦各位大大帮忙看下,为什么报错:未能找到路径“C:\Users\Administrator\Desktop\新建文件夹\2015-03-01\CBL7501A\LTE-TDD\1.txt”的一部分
------解决思路----------------------
public void Download(string FileName, string ftpUrl, string path,int FileSize)
        {
            FtpWebRequest reqFTP;
            try
            {
Directory.CreateDirectory(path);
  相关解决方案