How can I check if directory C:/
contains a folder named MP_Upload
, and if it does not exist, create the folder automatically?
I am using Visual Studio 2005 C#.
How can I check if directory C:/
contains a folder named MP_Upload
, and if it does not exist, create the folder automatically?
I am using Visual Studio 2005 C#.
Directory.CreateDirectory does exactly what you want: It creates the directory if it does not exist yet. There's no need to do an explicit check first.
(This also means that all directories along the path are created if needed:
CreateDirectory(@"C:\a\b\c\d")
suffices, even ifC:\a
does not exist yet.)Let me add a word of caution about your choice of directory, though: Creating a folder directly below the system partition root
C:\
is frowned upon. Consider letting the user choose a folder or creating a folder in%APPDATA%
or%LOCALAPPDATA%
instead (use Environment.GetFolderPath for that). The MSDN page of the Environment.SpecialFolder enumeration contains a list of special operating system folders and their purposes.This should help:
You can try this..
This should work