檢查目錄中是否存在一個文件夾,並使用c#創建它們。

[英]Check if a folder exist in a directory and create them using C#


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#。

6 个解决方案

#1


156  

This should help:

這應該有助於:

using System.IO;
...

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

#2


152  

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.

目錄中。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中指定的任何和所有目錄都是創建的,除非它們已經存在,或者路徑的某些部分無效。路徑參數指定目錄路徑,而不是文件路徑。如果目錄已經存在,則此方法不執行任何操作。

(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.)

(這也意味着如果需要的話,路徑上的所有目錄都是創建的:CreateDirectory(@“C:\ \ \ \ \ 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.GetFolderPath for that). The MSDN page of the Environment.SpecialFolder enumeration contains a list of special operating system folders and their purposes.

不過,請允許我對您選擇的目錄添加一個警告:在系統分區根C下面直接創建一個文件夾:\是不贊成的。考慮讓用戶選擇一個文件夾,或者在%APPDATA%或%LOCALAPPDATA%中創建一個文件夾(使用環境)。GetFolderPath)。環境的MSDN頁面。SpecialFolder枚舉包含一個特殊操作系統文件夾及其用途的列表。

#3


9  

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

#4


5  

This should work

這應該工作

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

#5


0  

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

#6


0  

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

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2012/02/01/730193d81993d5df8613f8e13d6147a6.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com