Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# File.Copy throws exception "The given path's format is not supported"

Tags:

c#

.net

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

Produces this exception:

The given path's format is not supported.

Stack trace:

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

Both the source and destination directories exist.

like image 709
Dave Avatar asked Dec 17 '12 16:12

Dave


2 Answers

You have colons (:) in the destination path, which Windows does not allow (apart from as part of the drive letter specifier, of course...).

To get this to work, pick a different date format for the date-time you are trying to embed, which does not use colons.

like image 169
David M Avatar answered Oct 10 '22 08:10

David M


Here is a MSDN site which will explain reserved characters as well as what is allowed or not allowed when creating Windows File Paths replace the ":" with underscores if you have to "_" reformat the date portion as well to use yyyymmdd, or mmddyyyy stored in a different DateTime variable and convert that to a String.. there are many other options you can use..

Naming Files, Paths, and Namespaces (Windows)

like image 36
MethodMan Avatar answered Oct 10 '22 09:10

MethodMan