File.Move,为什么我会收到 FileNotFoundException?文件存在
这非常奇怪,因为程序正在迭代文件! outfolder 和 infolder 都位于 H:/ 我使用 Windows 7 的外部硬盘中。这个想法是移动仅包含扩展名为 db 和 svn-base 的文件的所有文件夹。当我尝试移动文件夹时,出现异常。 VS2010告诉我它找不到dir中指定的文件夹。这段代码正在遍历 dir 所以它怎么找不到它!这很奇怪。
string []theExt = new string[] { "db", "svn-base" };
foreach (var dir in Directory.GetDirectories(infolder))
{
bool hit = false;
if (Directory.GetDirectories(dir).Count() > 0)
continue;
foreach (var f in Directory.GetFiles(dir))
{
var ext = Path.GetExtension(f).Substring(1);
if(theExt.Contains(ext) == false)
{
hit = true;
break;
}
}
if (!hit)
{
var dst = outfolder + "\\" + Path.GetFileName(dir);
File.Move(dir, outfolder); //FileNotFoundException: Could not find file dir.
}
}
}
Its extremely weird since the program is iterating the file! outfolder and infolder are both in H:/ my external HD using windows 7. The idea is to move all folders that only contain files with the extention db and svn-base. When i try to move the folder i get an exception. VS2010 tells me it cant find the folder specified in dir. This code is iterating through dir so how can it not find it! this is weird.
string []theExt = new string[] { "db", "svn-base" };
foreach (var dir in Directory.GetDirectories(infolder))
{
bool hit = false;
if (Directory.GetDirectories(dir).Count() > 0)
continue;
foreach (var f in Directory.GetFiles(dir))
{
var ext = Path.GetExtension(f).Substring(1);
if(theExt.Contains(ext) == false)
{
hit = true;
break;
}
}
if (!hit)
{
var dst = outfolder + "\\" + Path.GetFileName(dir);
File.Move(dir, outfolder); //FileNotFoundException: Could not find file dir.
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信您正在尝试使用 文件移动整个目录。 Move 需要一个文件名。
尝试使用 Directory.Move 代替,因为允许您可以移动整个文件夹。
I believe you are trying to move a whole directory using File.Move which expects a filename.
Try using Directory.Move instead since that allow you to move entire folders around.