2012-12-17 82 views
3
string source = @"C:\Users\damanja\Desktop\Projects\RecStudentManagement\RecStudentManagement.Web\Reports\Templates\ContactInformationReport.xlsx"; 
string dest = @"C:\Users\damanja\Desktop\Projects\RecStudentManagement\RecStudentManagement.Web\Reports\Ran\damanja2012-12-17T10:14:02.0394885-06:00.xlsx"; 

File.Copy(source, dest, true); 

產生以下異常:C#File.Copy拋出異常 「不支持給定的路徑的格式爲」

The given path's format is not supported. 

堆棧跟蹤:

at System.Security.Util.StringExpressionSet.CanonicalizePath(String path, Boolean needFullPath) 
    at System.Security.Util.StringExpressionSet.CreateListFromExpressions(String[] str, Boolean needFullPath) 
    at System.Security.Permissions.FileIOPermission.AddPathList(FileIOPermissionAccess access, AccessControlActions control, String[] pathListOrig, Boolean checkForDuplicates, Boolean needFullPath, Boolean copyPathList) 
    at System.Security.Permissions.FileIOPermission..ctor(FileIOPermissionAccess access, String[] pathList, Boolean checkForDuplicates, Boolean needFullPath) 
    at System.IO.File.InternalCopy(String sourceFileName, String destFileName, Boolean overwrite) 
    at System.IO.File.Copy(String sourceFileName, String destFileName, Boolean overwrite) 
    at RecStudentManagement.Web.Reports.ExcelReport.CopyTemplate() in C:\\Users\\damanja\\Desktop\\Projects\\RecStudentManagement\\RecStudentManagement.Web\\Reports\\ExcelReport.cs:line 52 
    at RecStudentManagement.Web.Reports.ExcelReport..ctor(String fileName) in C:\\Users\\damanja\\Desktop\\Projects\\RecStudentManagement\\RecStudentManagement.Web\\Reports\\ExcelReport.cs:line 35 
    at RecStudentManagement.Web.Reports.ContactInformationReport..ctor(IEnumerable`1 students, IEnumerable`1 includedPrograms, String createdByULID, String fileName) in C:\\Users\\damanja\\Desktop\\Projects\\RecStudentManagement\\RecStudentManagement.Web\\Reports\\ContactInformationReport.cs:line 22 
    at RecStudentManagement.Web.Controllers.ReportsController.ContactInformationCreate(ContactInformationSetUpViewModel vm) in C:\\Users\\damanja\\Desktop\\Projects\\RecStudentManagement\\RecStudentManagement.Web\\Controllers\\ReportsController.cs:line 99 

的源和目標目錄存在。

回答

21

您在目標路徑中有冒號(:),Windows不允許(除了作爲驅動器字母說明符的一部分,當然......)。

爲了得到這個工作,爲您想要嵌入的日期時間選擇不使用冒號的日期格式。

3

這裏是一個MSDN網站,其中解釋了保留字符以及創建Windows文件路徑 更換當什麼是允許或不允許「:」用下劃線,如果你要「_」格式化日期部分以及使用YYYYMMDD,或MMDDYYYY存儲在不同的日期時間變量,將其轉換成字符串。還有,你可以使用許多其他的選擇..

Naming Files, Paths, and Namespaces (Windows)

相關問題