C# 检查目录中是否存在文件夹,并使用C创建它们#
如何检查目录C# 检查目录中是否存在文件夹,并使用C创建它们#,c#,asp.net,visual-studio,C#,Asp.net,Visual Studio,如何检查目录C://是否包含名为MP\u Upload的文件夹,如果该文件夹不存在,则自动创建该文件夹 我正在使用VisualStudio2005 C#。这将有助于: using System.IO; ... string path = @"C:\MP_Upload"; if(!Directory.Exists(path)) { Directory.CreateDirectory(path); } 做您想要的事情:如果目录还不存在,它会创建目录无需先进行明确检查。 将创建路径中指定的
C://
是否包含名为MP\u Upload
的文件夹,如果该文件夹不存在,则自动创建该文件夹
我正在使用VisualStudio2005 C#。这将有助于:
using System.IO;
...
string path = @"C:\MP_Upload";
if(!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
做您想要的事情:如果目录还不存在,它会创建目录无需先进行明确检查。
将创建路径中指定的任何和所有目录,除非它们已存在或路径的某些部分无效。path参数指定目录路径,而不是文件路径。如果目录已经存在,则此方法不执行任何操作
(这也意味着如果需要,将创建路径上的所有目录:CreateDirectory(@“C:\a\b\C\d”)
就足够了,即使C:\a
还不存在。)
不过,让我补充一句关于目录选择的警告:不赞成在系统分区根目录下直接创建文件夹
C:\
。考虑让用户选择文件夹或在<代码> %AppDATION%或<代码> %LoalAppDATAs%中创建文件夹。枚举的MSDN页面包含特殊操作系统文件夹及其用途的列表。这应该可以
if(!Directory.Exists(@"C:\MP_Upload")) {
Directory.CreateDirectory(@"C:\MP_Upload");
}
使用系统;
使用System.IO;
使用System.Windows.Forms;
命名空间目录组合
{
公共部分类组合:表单
{
private const string_Path=@“D:/folder1/folder2/folfer3/folder4/file.txt”;
私有字符串_finalPath=null;
私有字符串_error=null;
公共关系组合()
{
初始化组件();
if(!FSParse(_Path))
控制台写入线(_错误);
其他的
Console.WriteLine(_finalPath);
}
私有bool FSParse(字符串路径)
{
尝试
{
string[]Splited=path.Replace(@“/”,@“/”).Replace(@“\\”,@“/”).Replace(@“\”,“/”).Split(“:”);
字符串NewPath=Splited[0]+“:”;
if(Directory.Exists(NewPath))
{
字符串[]路径=拆分的[1]。子字符串(1)。拆分('/');
for(int i=0;i
其他几个线程也出现了这种情况。尽管您不需要检查,但它确实使代码的意图更加清晰,对于局外人来说,可读性也越来越强。所以保持检查可能是一件好事。@MattJ:在这种情况下,我宁愿添加一个简短的注释,而不是一个无用的函数调用。我同意这种行为并不明显,但另一方面,命名它(更合适)ensureredirectoryexists
会使该方法更难找到。需要注意的是:Directory.CreateDirectory
将在文件夹名与现有文件名匹配时抛出。
using 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);
}