Click here to Skip to main content
15,920,438 members
Please Sign up or sign in to vote.
2.09/5 (3 votes)
See more:
Hi,

I have create a windows form with button.
I need to check whether the path (eg, c:\test\) can have folder are not. If not means create a new folder called 1_date stamp (eg 1_20120711).

If yes means create a auto increment folder (ie. 2_20120711).

Please help me.

Thanks,
Murugavel S
Posted
Updated 9-Dec-18 20:16pm

Try

string foldername = DateTime.Now.ToString();
C#
if (!Directory.Exists(@"C:/test/" + foldername)
       Directory.CreateDirectory(@"C:/test/" + foldername);


The folder name is based on date - however it is not in your format.

If a folder with the date already exists, create a new one with an incremented number,
Based on the code above, you can always try this on your own.
 
Share this answer
 
Comments
Prasad_Kulkarni 11-Jul-12 6:00am    
Yep. To the point +5!
Abhinav S 11-Jul-12 6:01am    
Thanks Prasadd.
Shemeer NS 11-Jul-12 6:04am    
5'ed..
Abhinav S 11-Jul-12 7:38am    
Thanks.
Rahul Rajat Singh 11-Jul-12 6:22am    
Perfect. +5.
you can used :-
Quote:
foreach(string path in args)
{
if(File.Exists(path))
{
// This path is a file
ProcessFile(path);
}
else if(Directory.Exists(path))
{
// This path is a directory
ProcessDirectory(path);
}
else
{
Console.WriteLine("{0} is not a valid file or directory.", path);
}
}
}

and you study more into following linq;-
http://msdn.microsoft.com/en-us/library/system.io.directory.exists.aspx[^]
 
Share this answer
 
use
System.IO.Directory.CreateDirectory


Directory.CreateDirectory(@"C:/test/" + foldername);


CreateDirectory already handles the check if the directory does not exists
 
Share this answer
 
v2
Comments
CHill60 10-Dec-18 8:48am    
"CreateDirectory already handles the check if the directory does not exists" … No it does not, it just doesn't create a new folder, nor does it throw an exception.

If the folder already exists the OP wanted the name to increment. Which is why the check to see if it already exists is necessary to solve the OP's 6 year old problem

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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