简体   繁体   English

FileNotFoundException尝试将文件从一个目录复制到另一个目录时

[英]FileNotFoundException While trying to copy the file from one directory to another

I have some files (.txt, word, excel files) in "C:\\ABC\\Temp" and I want to move all the .txt files into "C:\\ABC\\Text", but I'm getting a FileNotFoundException . 我在“ C:\\ ABC \\ Temp”中有一些文件(.txt,word,excel文件),并且我想将所有.txt文件都移到“ C:\\ ABC \\ Text”中,但是我得到了FileNotFoundException

Please find attached code. 请找到随附的代码。

static void Main()
{
    string sp = @"C:/ABC/Temp";
    string dp = @"C:/ABC/TextFiles";
    string[] fileList=Directory.GetFiles(sp);

    foreach(string file in fileList)
    {
        if (file.EndsWith(".txt"))
        { 
            File.Move(sp,dp);
        }
    }
}
}

} }

You're trying to move the entire directories with File.Move . 您正在尝试使用File.Move移动整个目录。

You have to specify the file name as well: 您还必须指定文件名:

File.Move(file, Path.Combine(dp, Path.GetFileName(file)));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM