找不到C#File.Copy目录

时间:2017-08-30 13:25:44

标签: c# .net

我正在尝试在C#中执行File.Copy

我得到DirectoryNotFoundException。但是,我验证了该目录的存在。

我的代码:

try 
{
    log.Info("Copying file from '" + srcFile + "' to '" + destFile + "'");
    log.Info("Source File: '" + srcFile + "' exists: " + File.Exists(srcFile));
    File.Copy(srcFile, destFile, true);
    log.Info("Copied file to '" + destFile + "' success");
}
catch (System.Exception ex)
{
    log.Error("Send Logs: Log file copy error", ex);
}

运行代码的结果:

2017-08-30 09:20:25,933 [1] INFO  [ArbitrationForums.AFClient.AFSystemTray.MainForm] Copying file from 'C:\Users\ptenn\AppData\Roaming\ArbitrationForums\logs\AFDashboard.log' to 'C:\Users\ptenn\AppData\Roaming\ArbitrationForums\AFClient\DataSpool\AFDashboard.log'
2017-08-30 09:20:25,933 [1] INFO  [ArbitrationForums.AFClient.AFSystemTray.MainForm] Source File: 'C:\Users\ptenn\AppData\Roaming\ArbitrationForums\logs\AFDashboard.log' exists: True
2017-08-30 09:20:25,934 [1] ERROR [ArbitrationForums.AFClient.AFSystemTray.MainForm] Send Logs: Log file copy error
System.IO.DirectoryNotFoundException: Could not find a part of the path 
    'C:\Users\ptenn\AppData\Roaming\ArbitrationForums\logs\AFDashboard.log'.
    at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
    at System.IO.File.InternalCopy(String sourceFileName, String 
    destFileName, Boolean overwrite, Boolean checkHost)
    at ArbitrationForums.AFClient.AFSystemTray.MainForm.sendLogs()

我应该拥有权限,因为我以用户身份登录AppData\Roaming文件夹。

在智慧结束时,有没有人有任何想法或建议?

非常感谢!

1 个答案:

答案 0 :(得分:1)

我和Timamonium在这一个上。我建议您的目标目录不存在" C:\ Users \ ptenn \ AppData \ Roaming \ ArbitrationForums \ AFClient \ D ataSpool \"