2017-08-30 77 views
1

我想在C#中執行File.CopyC#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文件夾的用戶。

有趣的是,有沒有人有任何想法或建議?

非常感謝!

+0

擺脫'File.Exists()'檢查。這真的沒有幫助你。 –

+0

您是否檢查過[documentation](https://msdn.microsoft.com/library/c6cfw35a(v = vs.110).aspx)? * sourceFileName或destFileName中指定的路徑無效(例如,它位於未映射的驅動器上)。* –

+1

*驗證目錄'C:\ Users \ ptenn \ AppData \ Roaming \ ArbitrationForums \ logs \ *您的所有**用戶 –

回答

1

我與Timamonium在這一個。我建議您的目標目錄不存在「C:\ Users \ ptenn \ AppData \ Roaming \ ArbitrationForums \ AFClient \ D ataSpool \」