Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey guys,

Now i want to ask you someting about creating directory. I know how to create dir with names that i write in my code but i want to create directory that name is date of today. Here is my code;

C#
DateTime now1 = new DateTime();
           now1 = DateTime.Today;
           string bugun = now1.ToString();


           if (!Directory.Exists(@"c:\data\" + bugun))
               Directory.CreateDirectory(@"c:\data\" + bugun);



The error code is "An unhandled exception of type 'System.NotSupportedException' occured in mscore.lib" Something is wrong about directory codes.
Posted
Comments
Maciej Los 12-Dec-14 6:17am    
What is your date separator?
I'd suggest to use something like this:
string myDate = DateTime.Today.ToString("yyyyMMdd");
Directory.CreateDirectory(initialDir + myDate);

If you use a debugger it says: The given path's format is not supported.

bugun is 12.12.2014 00:00:00

So you are trying to create a path called c:\data\12.12.2014 00:00:00

':' is not allowed in a path name.

Change the format of your date e.g.:
C#
string bugun = now1.ToString("yyyy-MM-dd");


The resulting path will be: C:\data\2014-12-12
 
Share this answer
 
v3
Comments
BillWoodruff 12-Dec-14 12:14pm    
+5
TheRealSteveJudge 15-Dec-14 2:51am    
Thank you!
You should use the following code to combine strings into a path

colorPath = System.IO.Path.Combine(@"c:\data\", bugun);


The Combine method takes care of adding or removing backslashes where necessary.
DateTime.Today returning date which contains '/',spaces and/or special character. Combine method will take care of that.
 
Share this answer
 
Comments
Maciej Los 12-Dec-14 6:24am    
+5!
TheRealSteveJudge 12-Dec-14 6:29am    
I don't know what has happened, but your example actually works! 5*
Praveen Kumar Upadhyay 12-Dec-14 6:31am    
Thank you guys for Upvoting my solution.
TheRealSteveJudge 12-Dec-14 6:56am    
Unfortunately your code results in an 'System.NotSupportedException' exception.
It still says: "The given path's format is not supported"
I thought it worked, because I still had "string bugun = now1.ToString("yyyy-MM-dd");"
in my code.
C#
string bugun = DateTime.Now.ToString("yyyyMMdd"); 
if (!Directory.Exists(@"c:\data\" + bugun))
{
   Directory.CreateDirectory(@"c:\data\" + bugun);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900