2012-12-17 56 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)

相关问题