File.Move,为什么我得到FileNotFoundException?该文件存在

0 .net filenotfoundexception

由于程序正在迭代文件,因此非常奇怪!outfolder和infolder都在H:/我的外部HD使用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.
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

Isa*_*avo 6

我相信您正在尝试使用File.Move移动整个目录,该文件需要文件名.

尝试使用Directory.Move,因为它允许您移动整个文件夹.