File.Copy()失败或不执行,程序不能使用假定要复制的文件

时间:2014-10-21 01:07:20

标签: c# .net

我目前有一个简单的程序来截取屏幕截图,将截屏保存到当前文件夹(exe所在的位置),然后将该文件复制到存档文件夹中。这就是我所拥有的。

public const string ext = "png";
public static int id = new Random().Next(1, 999999999);
public const string archivefolder = "AnonShot Screenshot Archives";
public static string archivepath = info.path + "\\" + info.archivefolder;
public static string filename = "Screenshot-" + info.id.ToString();
public static string path = Directory.GetCurrentDirectory();
public static string fullpath = info.archivepath + "\\" + info.filename + "." + info.ext;

...

// Copy the screenshot into the archive folder
string source = info.path + "\\" + info.filename + "." + info.ext;
string destination = info.archivepath;
File.Copy(source, destination + Path.GetFileName(source));

(整个文件可以在http://pastebin.com/Fr9yzJcZ找到)

保存文件有效(例如截屏-123456789.png),但是当它应该将该文件复制到存档文件夹时,它会出错

  

System.UnauthorizedAccessException:找不到路径的一部分'\ AnonShot Screenshot Archives \ Screenshot-123456789.png'。

那是因为文件尚未复制到该文件夹​​,因为File.Copy命令没有完成它的工作,所以我相信。

1 个答案:

答案 0 :(得分:0)

这个问题是我所知道的最好的程序试图找到字面上的“\ Anon ...”而不是“C:\ Users ...”

让代码通过Directory.GetCurrentDirectory()获取当前目录为我解决了问题。

相关问题