FTP下载文件问题,急!!!!

鬼五拾柒 2015-04-09 05:07:28
       
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”的一部分
...全文
100 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
slwsss 2015-04-09
  • 打赏
  • 举报
回复
public void Download(string FileName, string ftpUrl, string path,int FileSize) { FtpWebRequest reqFTP; try { Directory.CreateDirectory(path);
鬼五拾柒 2015-04-09
  • 打赏
  • 举报
回复
引用 3 楼 slwsss 的回复:
这个不会给你创建目录 [quote=引用 2 楼 u011683479 的回复:] [quote=引用 1 楼 slwsss 的回复:] 没创建目录?
FileStream outputStream = new FileStream(path + "\\" + FileName, FileMode.Create);不是有这句吗??存在文件就覆盖,不存在就创建[/quote][/quote] 那怎么改呢
slwsss 2015-04-09
  • 打赏
  • 举报
回复
这个不会给你创建目录
引用 2 楼 u011683479 的回复:
[quote=引用 1 楼 slwsss 的回复:] 没创建目录?
FileStream outputStream = new FileStream(path + "\\" + FileName, FileMode.Create);不是有这句吗??存在文件就覆盖,不存在就创建[/quote]
鬼五拾柒 2015-04-09
  • 打赏
  • 举报
回复
引用 1 楼 slwsss 的回复:
没创建目录?
FileStream outputStream = new FileStream(path + "\\" + FileName, FileMode.Create);不是有这句吗??存在文件就覆盖,不存在就创建
slwsss 2015-04-09
  • 打赏
  • 举报
回复
没创建目录?

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧