使用File.Copy复制文件后,当前目录列表不包含复制的文件:
string path = Directory.GetCurrentDirectory();
string[] files = Directory.GetFiles(path);
Console.WriteLine("Enter the name of the file to copy");
source = Console.ReadLine();
Console.WriteLine("Enter a name for the new file:");
string target = Console.ReadLine();
File.Copy(source, target);
Console.WriteLine("\n\nNew listing:");
foreach (var file in files)
Console.WriteLine(file);
答案 0 :(得分:6)
files
将不会更新,直到您再次致电files = Directory.GetFiles(path);
。
这是一个从对GetFiles
的调用中填充的数组,而不是目录本身的链接。
答案 1 :(得分:0)
将代码更改为此:
string path = Directory.GetCurrentDirectory();
string[] files = Directory.GetFiles(path);
Console.WriteLine("Enter the name of the file to copy");
source = Console.ReadLine();
Console.WriteLine("Enter a name for the new file:");
string target = Console.ReadLine();
File.Copy(source, target);
Console.WriteLine("\n\nNew listing:");
files = Directory.GetFiles(path); //this added
foreach (var file in files)
Console.WriteLine(file);