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

本文关键字:创建 是否 文件夹 存在 检查 | 更新日期: 2023-09-27 18:22:09

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

我正在使用Visual;工作室;2005年C。

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

这应该会有所帮助:

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

Directory.CreateDirectory正是您想要的:如果目录还不存在,它会创建该目录没有必要先进行明确的检查

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

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


不过,让我补充一句关于您选择目录的警告:在系统分区根目录C:'正下方创建文件夹是不可取的。请考虑让用户选择一个文件夹,或者在%APPDATA%%LOCALAPPDATA%中创建一个文件夹(请使用Environment.GetFolderPath)。Environment.SpecialFolder枚举的MSDN页包含特殊操作系统文件夹及其用途的列表。

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

这应该能在中工作

if(!Directory.Exists(@"C:'MP_Upload")) {
    Directory.CreateDirectory(@"C:'MP_Upload");
}
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;
            }
        }
    }
}
    String path = Server.MapPath("~/MP_Upload/");
    if (!Directory.Exists(path))
    {
        Directory.CreateDirectory(path);
    }