简体   繁体   中英

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 .

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 .

You have to specify the file name as well:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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