Check if a folder exist in a directory and create them using C#
C#asp.netVisual StudioC# Problem Overview
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#.
C# Solutions
Solution 1 - C#
This should help:
using System.IO;
...
string path = @"C:\MP_Upload";
if(!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
Solution 2 - C#
using System.IO;
...
Directory.CreateDirectory(@"C:\MP_Upload");
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.
> Any and all directories specified in path are created, unless they already exist or unless some part of path is invalid. The path parameter specifies a directory path, not a file path. If the directory already exists, this method does nothing.
(This also means that all directories along the path are created if needed: CreateDirectory(@"C:\a\b\c\d")
suffices, even if C:\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.
Solution 3 - C#
if(!System.IO.Directory.Exists(@"c:\mp_upload"))
{
System.IO.Directory.CreateDirectory(@"c:\mp_upload");
}
Solution 4 - C#
This should work
if(!Directory.Exists(@"C:\MP_Upload")) {
Directory.CreateDirectory(@"C:\MP_Upload");
}
Solution 5 - C#
using System;
using System.IO;
using System.Windows.Forms;
namespace DirCombination
{
public partial class DirCombination : Form
{
private const string _Path = @"D:/folder1/foler2/folfer3/folder4/file.txt";
private string _finalPath = null;
private string _error = null;
public DirCombination()
{
InitializeComponent();
if (!FSParse(_Path))
Console.WriteLine(_error);
else
Console.WriteLine(_finalPath);
}
private bool FSParse(string path)
{
try
{
string[] Splited = path.Replace(@"//", @"/").Replace(@"\\", @"/").Replace(@"\", "/").Split(':');
string NewPath = Splited[0] + ":";
if (Directory.Exists(NewPath))
{
string[] Paths = Splited[1].Substring(1).Split('/');
for (int i = 0; i < Paths.Length - 1; i++)
{
NewPath += "/";
if (!string.IsNullOrEmpty(Paths[i]))
{
NewPath += Paths[i];
if (!Directory.Exists(NewPath))
Directory.CreateDirectory(NewPath);
}
}
if (!string.IsNullOrEmpty(Paths[Paths.Length - 1]))
{
NewPath += "/" + Paths[Paths.Length - 1];
if (!File.Exists(NewPath))
File.Create(NewPath);
}
_finalPath = NewPath;
return true;
}
else
{
_error = "Drive is not exists!";
return false;
}
}
catch (Exception ex)
{
_error = ex.Message;
return false;
}
}
}
}
Solution 6 - C#
String path = Server.MapPath("~/MP_Upload/");
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}