C# 未能找到路径“D:\abc\”的一部分

majinghai_1988 2018-08-26 03:48:57
public static string DownloadFtp(string filePath, string fileName, string ftpServerIP, string ftpUserID, string ftpPassword)
{
FtpWebRequest reqFTP;
string fn = @"D:\\abc\" + Console.ReadLine(); ;
try
{
//filePath = < <The full path where the file is to be created.>>,
//fileName = < <Name of the file to be created(Need not be the name of the file on FTP server).>>

if (!Directory.Exists(fn))
{
Directory.CreateDirectory(fn);
}
FileStream outputStream = new FileStream(fn, FileMode.Create);

reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + filePath + "/" + fileName));
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
reqFTP.KeepAlive = false;
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream ftpStream = response.GetResponseStream();
long cl = response.ContentLength;
int bufferSize = 500000000;
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();
return "ftp://" + ftpServerIP + filePath + "/" + fileName + "--------" + fn;
}
catch (Exception ex)
{
// Logging.WriteError(ex.Message + ex.StackTrace);
// System.Windows.Forms.MessageBox.Show(ex.Message);
Console.WriteLine(ex.Message);
return "Exception ftp://" + ftpServerIP + filePath + "/" + fileName + "------"+ex.Message+"--------"+fn;
}
}


通过在FTP下载指定文件,并保存在本地d:/abc则个文件夹,提示未能找到路径“D:\abc\”的一部分,确认D:\abc\是存在的,不存在的情况也会自动新建,且已经手动设置abc文件夹权限为EveryOne所有权限
...全文
2049 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
gdssq 2018-08-27
  • 打赏
  • 举报
回复
1.引用 System.IO。 2.string dirpath =...//具体自己添。 3.string filepath=...//具体自己添。 4.if (Directory.Exists(path))//判断目录是否存在。 5.if(File.Exist(filepath))//如果是文件的话
吉普赛的歌 2018-08-27
  • 打赏
  • 举报
回复
目录和文件都分不清么?
先不要写这个方法了, 先写一个控制台程序, 里面的路径、文件全部用实际的来代替, 成功了再改为方法, 这样心里就有数了。
xuzuning 2018-08-27
  • 打赏
  • 举报
回复
新手就该多看手册,而不是想当然的胡来
这样就不易犯“老手”的习惯性错误
xuzuning 2018-08-27
  • 打赏
  • 举报
回复
FileMode.Create 就是不管文件存在与否都创建
majinghai_1988 2018-08-27
  • 打赏
  • 举报
回复
引用 2 楼 xuzuning 的回复:
你有 Directory.CreateDirectory(fn);
显然 fn 是目录名,而将目录以文件流方式打开,当然是不行的


这里加个文件名可以了,怎么判断是否已经存在文件了,如果存在文件就覆盖呢?
majinghai_1988 2018-08-27
  • 打赏
  • 举报
回复
引用 2 楼 xuzuning 的回复:
你有 Directory.CreateDirectory(fn);
显然 fn 是目录名,而将目录以文件流方式打开,当然是不行的


我是新手,那我应该怎么改呢?
xuzuning 2018-08-27
  • 打赏
  • 举报
回复
你有 Directory.CreateDirectory(fn);
显然 fn 是目录名,而将目录以文件流方式打开,当然是不行的
  • 打赏
  • 举报
回复

string fn = "D:\\abc\\" + Console.ReadLine();
string fn = @"D:\abc\" + Console.ReadLine();;



没仔细看代码可能是这里的事情

110,536

社区成员

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

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

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