1
我想在C#中执行File.Copy
。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
文件夹的用户。
有趣的是,有没有人有任何想法或建议?
非常感谢!
摆脱'File.Exists()'检查。这真的没有帮助你。 –
您是否检查过[documentation](https://msdn.microsoft.com/library/c6cfw35a(v = vs.110).aspx)? * sourceFileName或destFileName中指定的路径无效(例如,它位于未映射的驱动器上)。* –
*验证目录'C:\ Users \ ptenn \ AppData \ Roaming \ ArbitrationForums \ logs \ *您的所有**用户 –