检查目录中是否存在文件夹并使用C#创建它们

时间:2012-02-01 07:12:12

标签: c# asp.net visual-studio

如何检查目录C:/是否包含名为MP_Upload的文件夹,如果该文件夹不存在,请自动创建该文件夹?

我正在使用Visual Studio 2005 C#。

8 个答案:

答案 0 :(得分:180)

这应该有所帮助:

using System.IO;
...

string path = @"C:\MP_Upload";
if(!Directory.Exists(path))
{
    Directory.CreateDirectory(path);
}

答案 1 :(得分:164)

using System.IO;
...

Directory.CreateDirectory(@"C:\MP_Upload");

Directory.CreateDirectory完全符合您的要求:如果目录尚不存在,它会创建目录。 无需先进行明确检查。

  

创建路径中指定的任何和所有目录,除非它们已存在或除非某些路径部分无效。 path参数指定目录路径,而不是文件路径。如果该目录已存在,则此方法不执行任何操作。

(这也意味着如果需要,将创建沿路径的所有目录CreateDirectory(@"C:\a\b\c\d")就足够了,即使C:\a尚不存在。)


让我对您选择的目录添加一个警告,但是:在系统分区根C:\正下方创建一个文件夹是不受欢迎的。考虑让用户选择文件夹或在%APPDATA%%LOCALAPPDATA%中创建文件夹(请使用Environment.GetFolderPath)。 Environment.SpecialFolder枚举的MSDN页面包含特殊操作系统文件夹及其用途的列表。

答案 2 :(得分:11)

if(!System.IO.Directory.Exists(@"c:\mp_upload"))
{
     System.IO.Directory.CreateDirectory(@"c:\mp_upload");
}

答案 3 :(得分:6)

这应该有效

if(!Directory.Exists(@"C:\MP_Upload")) {
    Directory.CreateDirectory(@"C:\MP_Upload");
}

答案 4 :(得分:2)

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;
            }
        }
    }
}

答案 5 :(得分:1)

    String path = Server.MapPath("~/MP_Upload/");
    if (!Directory.Exists(path))
    {
        Directory.CreateDirectory(path);
    }

答案 6 :(得分:0)

您可以尝试一下。

using System.IO;string path = "C:\MP_Upload";if(!Directory.Exists(path)){
   Directory.CreateDirectory(path);}

答案 7 :(得分:0)

您可以使用以下代码检查目录是否存在

if(!Directory.Exists(@"C:\MP_Upload")) {
    //do anything
}

您可以使用以下代码检查该目录中是否存在任何文件

System.IO.File.Exists(path)