Csharp/C#教程:C#File.Copy抛出exception“不支持给定路径的格式”分享


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

string source = @"C:UsersdamanjaDesktopProjectsRecStudentManagementRecStudentManagement.WebReportsTemplatesContactInformationReport.xlsx"; string dest = @"C:UsersdamanjaDesktopProjectsRecStudentManagementRecStudentManagement.WebReportsRandamanja2012-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)

上述就是C#学习教程:C#File.Copy抛出exception“不支持给定路径的格式”分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/936169.html

(0)
上一篇 2021年11月6日
下一篇 2021年11月6日

精彩推荐