C#File.Copy抛出exception“不支持给定路径的格式”

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); 

产生此exception:

 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 

源目录和目标目录都存在。

您在目标路径中有冒号(:),Windows不允许(当然,除了作为驱动器号说明符的一部分…)。

要使其工作,请为您尝试嵌入的日期时间选择不同的日期格式,这不使用冒号。

这是一个MSDN站点,它将解释保留字符以及创建Windows文件路径时允许或不允许的内容如果必须“_”重新格式化日期部分以及使用yyyymmddmmddyyyy ,则使用下划线替换“:”存储在不同的DateTime变量中并将其转换为String ..您可以使用许多其他选项..

命名文件,路径和命名空间(Windows)