2013-04-23 73 views
2

您好我正在嘗試將文件複製到共享網絡,但我繼續收到錯誤The filename, directory name, or volume label syntax is incorrect。有人可以請你糾正我做錯了什麼。此外我的應用程序正在運行使用ASP.NET MVC3,謝謝!

複製代碼
文件名,目錄名稱或卷標語法不正確 - 映射共享網絡驅動器(ASP.NET MVC3 C#)

File.Copy(@path, @Properties.Settings.Default.SharedMappedOutput); 

的路徑是一個參數:C:\log\12345.pdf
Properties.Settings.Default.SharedMappedOutput路徑:\\vfler_xx\evl_xx\VT\

我要完成什麼:我想將文件從Path(文件名)複製到SharedMappedOutput(目錄)


的額外信息

  • 的Windows Server 2008 R2
  • IIS 7.5
  • ASP.NET MVC3 C#

編輯
我已經改變了我的代碼以@Steve的幫助,但現在它說我沒有訪問,我沒有指定路徑。

String dest_path = Properties.Settings.Default.SharedMappedOutput; 
File.Copy(@path, Path.Combine(dest_path, Path.GetFileName(path))); 

錯誤
Access to the path 'c:\windows\system32\insetsrv\12345.pdf' is denied 我沒有具體說明這條道路,我不知道爲什麼它正試圖訪問此路徑。

鏈接到新的問題 https://stackoverflow.com/questions/16179585/iis-acccess-to-the-path-c-windows-system32-inetsrv-12345-pdf-is-denied


謝謝。請讓我知道,如果有任何問題或誤解的問題。再次感謝您。

回答

2

File.Copy不會將目錄複製到另一個目錄,而只是一個文件名。

c:\log似乎是一個目錄的只是名字和由documentation at MSDN說這不工作

複製所有文件中的源路徑,你可以寫這個

string destPath = Properties.Settings.Default.SharedMappedOutput; 
foreach (string aFile in Directory.GetFiles(path, "*.*")) 
    File.Copy(aFile, Path.Combine(destPath, Path.GetFileName(aFile))); 

編輯 看到關於真實源名稱的評論,那麼答案仍然是部分有效的,因爲目的地也應該是文件名而不是目錄。所以答案變成

string sourceFile = @"C:\log\12345.pdf"; 
string destPath = Properties.Settings.Default.SharedMappedOutput; 
File.Copy(sourceFile, Path.Combine(destPath, Path.GetFileName(sourceFile))); 
+0

對不起,這是我的一個錯字,第一個參數是文件名。謝謝你的回覆 – AustinT 2013-04-23 20:53:26

+0

好吧,然而,目標應該是一個文件,而不是一個路徑。看看我的回答如何構建目標文件名 – Steve 2013-04-23 21:01:28

+0

好的,所以當我處理一個文件而不是目錄時,它會是'File.Copy(path,Path.Combine(destPath,Path.GetFileName(path)));'那是對的嗎? – AustinT 2013-04-23 21:04:41

相關問題