Reputation: 502
What is wrong with the following statement?
XmlTextWriter writer = new XmlTextWriter(@"D:\project\data\" + System.DateTime.Today + @"\" + System.DateTime.Now + ".xml", null);
When I try the above statement it gives the following error
The given path's format is not supported.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NotSupportedException: The given path's format is not supported.
Upvotes: 1
Views: 3052
Reputation: 31454
Illegal characters aside, what you're trying to do is impossible for one simple reason: as long as XmlTextWriter
will create file if it doesn't exist, it won't create directory. And that's what you're trying to do:
XmlTextWriter writer = new XmlTextWriter(
/* your root path */ @"D:\project\data\" +
/* NEW directory */ System.DateTime.Today + @"\" +
/* new file name */ System.DateTime.Now + ".xml", null);
You either need to create directories for given day manually:
var path = string.Format(@"D:\project\data\{0:yyyyMMdd}", DateTime.Now);
// if directory already exists nothing will happen
Directory.CreateDirectory(path);
Or merge date into file name:
var fileName = string.Format(@"D:\project\data\{0:yyyyMMdd}_{0:HHmmssfff}.xml",
DateTime.Now);
Upvotes: 3
Reputation: 37205
Depending on your language settings (locale), the date or time format may contain illegal characters for a file name. For example, German time format contains a colon ':', and English date formats contain '/', both of which are not allowed in file or directory names.
Find out which illegal characters are generated by your locale, and either use an explicit format in DateTime.ToString(), or remove them by applying ToString().Replace(":", "") etc.
Upvotes: 1
Reputation: 2661
File names can't contain various symbols, :
is one of the illegal characters. Try using something like this instead:
String.Format(@"D:\project\data\{0:yyyyMMdd}\{0:HHmmssfff}.xml", DateTime.Now);
Upvotes: 2