检查目录中是否存在文件夹并使用 C# 创建它们
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9092160/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if a folder exist in a directory and create them using C#
提问by gymcode
How can I check if directory C:/contains a folder named MP_Upload, and if it does not exist, create the folder automatically?
如何检查目录是否C:/包含名为 的文件夹MP_Upload,如果不存在,则自动创建该文件夹?
I am using Visual Studio 2005 C#.
我正在使用 Visual Studio 2005 C#。
采纳答案by cycaHuH
This should help:
这应该有帮助:
using System.IO;
...
string path = @"C:\MP_Upload";
if(!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
回答by cycaHuH
if(!System.IO.Directory.Exists(@"c:\mp_upload"))
{
System.IO.Directory.CreateDirectory(@"c:\mp_upload");
}
回答by Heinzi
using System.IO;
...
Directory.CreateDirectory(@"C:\MP_Upload");
Directory.CreateDirectorydoes exactly what you want: It creates the directory if it does not exist yet. There's no need to do an explicit check first.
Directory.CreateDirectory完全符合您的要求:如果目录尚不存在,它会创建该目录。无需先进行显式检查。
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.
将创建 path 中指定的任何和所有目录,除非它们已经存在或除非 path 的某些部分无效。path 参数指定目录路径,而不是文件路径。如果目录已经存在,则此方法不执行任何操作。
(This also means that all directories along the pathare created if needed: CreateDirectory(@"C:\a\b\c\d")suffices, even if C:\adoes not exist yet.)
(这也意味着如果需要,路径上的所有目录都会被创建:CreateDirectory(@"C:\a\b\c\d")足够了,即使C:\a还不存在。)
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.GetFolderPathfor that). The MSDN page of the Environment.SpecialFolderenumeration contains a list of special operating system folders and their purposes.
不过,让我对您选择的目录加一句警告:在系统分区根目录下直接创建一个文件夹C:\是不受欢迎的。考虑让用户选择一个文件夹或在其中创建一个文件夹%APPDATA%或%LOCALAPPDATA%代替(为此使用Environment.GetFolderPath)。Environment.SpecialFolder枚举的 MSDN 页面包含特殊操作系统文件夹及其用途的列表。
回答by kufi
This should work
这应该工作
if(!Directory.Exists(@"C:\MP_Upload")) {
Directory.CreateDirectory(@"C:\MP_Upload");
}
回答by Azam Rahimjonov
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;
}
}
}
}
回答by Ronaldo Albertini
String path = Server.MapPath("~/MP_Upload/");
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
回答by Ashish
You can try this..
你可以试试这个..
using System.IO;string path = "C:\MP_Upload";if(!Directory.Exists(path)){
Directory.CreateDirectory(path);}

