C# 如果文件夹不存在,则创建它
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9065598/
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
If a folder does not exist, create it
提问by Tavousi
I use a FileUploader control in my application. I want to save a file in a specified folder. Now I want, if this folder does not exist, to first create it, and then save my file to this folder. If the folder already exists, then just save the file in it.
我在我的应用程序中使用 FileUploader 控件。我想将文件保存在指定的文件夹中。现在我想,如果这个文件夹不存在,首先创建它,然后将我的文件保存到这个文件夹中。如果文件夹已经存在,则只需将文件保存在其中。
How I can do this?
我怎么能做到这一点?
采纳答案by Mark Peters
As others have said, use System.IO.Directory.CreateDirectory
正如其他人所说,使用 System.IO.Directory.CreateDirectory
But, you don't need to check if it exists first. From the docs
但是,您不需要先检查它是否存在。从文档
Any and all directories specified in path are created, unless they already exist or unless some part of path is invalid. If the directory already exists, this method does not create a new directory, but it returns a DirectoryInfo object for the existing directory.
将创建 path 中指定的任何和所有目录,除非它们已经存在或除非 path 的某些部分无效。如果目录已存在,此方法不会创建新目录,但会返回现有目录的 DirectoryInfo 对象。
回答by Ravia
Use the below code as per http://forums.asp.net/p/1226236/2209871.aspx:
根据http://forums.asp.net/p/1226236/2209871.aspx使用以下代码:
string subPath ="ImagesPath"; // your code goes here
bool exists = System.IO.Directory.Exists(Server.MapPath(subPath));
if(!exists)
System.IO.Directory.CreateDirectory(Server.MapPath(subPath));
回答by jeroenh
Directory.CreateDirectoryExplains how to try and to create the FilePath if it does not exist
Directory.CreateDirectory解释如果 FilePath 不存在,如何尝试创建它
Directory.ExistsExplains how to check if a FilePath exists. However, you don't need this as CreateDirectory will check for you.
Directory.Exists解释如何检查 FilePath 是否存在。但是,您不需要它,因为 CreateDirectory 会为您检查。
回答by MethodMan
You can use a try/catch clause and check to see if it exist:
您可以使用 try/catch 子句并检查它是否存在:
try
{
if (!Directory.Exists(path))
{
// Try to create the directory.
DirectoryInfo di = Directory.CreateDirectory(path);
}
}
catch (IOException ioex)
{
Console.WriteLine(ioex.Message);
}
回答by Dennis Traub
You can create the path if it doesn't exist yet with a method like the following:
如果路径尚不存在,您可以使用如下方法创建路径:
using System.IO;
private void CreateIfMissing(string path)
{
bool folderExists = Directory.Exists(Server.MapPath(path));
if (!folderExists)
Directory.CreateDirectory(Server.MapPath(path));
}
回答by BlackBear
using System.IO
if (!Directory.Exists(yourDirectory))
Directory.CreateDirectory(yourDirectory);
回答by Nicolas Raoul
Just write this line :
只需写下这一行:
System.IO.Directory.CreateDirectory("my folder");
- If the folder does not exist yet, it will be created.
- If the folder exists already, the line will be ignored.
- 如果该文件夹尚不存在,则会创建它。
- 如果文件夹已经存在,该行将被忽略。
Reference: Article about Directory.CreateDirectory at MSDN
参考:MSDN 上关于 Directory.CreateDirectory 的文章
Of course, you can also write using System.IO;at the top of the source file and then just write Directory.CreateDirectory("my folder");every time you want to create a folder.
当然,你也可以using System.IO;在源文件的顶部写,然后Directory.CreateDirectory("my folder");每次要创建文件夹时都写。
回答by uksp
string root = @"C:\Temp";
string subdir = @"C:\Temp\Mahesh";
// If directory does not exist, create it.
if (!Directory.Exists(root))
{
Directory.CreateDirectory(root);
}
The CreateDirectory is also used to create a sub directory. All you have to do is to specify the path of the directory in which this subdirectory will be created in. The following code snippet creates a Mahesh subdirectory in C:\Temp directory.
CreateDirectory 还用于创建子目录。您所要做的就是指定将在其中创建此子目录的目录的路径。以下代码片段在C:\Temp directory.
// Create sub directory
if (!Directory.Exists(subdir))
{
Directory.CreateDirectory(subdir);
}
回答by Thakur Rock
This method will create folder if not exist and do nothing if exists
如果不存在,此方法将创建文件夹,如果存在则不执行任何操作
Directory.CreateDirectory(path);
回答by Lemon Kazi
Use below code. I used this code for file copy and create new folder.
使用下面的代码。我使用此代码进行文件复制并创建新文件夹。
string fileToCopy = "filelocation\file_name.txt";
String server = Environment.UserName;
string newLocation = "C:\Users\" + server + "\Pictures\Tenders\file_name.txt";
string folderLocation = "C:\Users\" + server + "\Pictures\Tenders\";
bool exists = System.IO.Directory.Exists(folderLocation);
if (!exists)
{
System.IO.Directory.CreateDirectory(folderLocation);
if (System.IO.File.Exists(fileToCopy))
{
MessageBox.Show("file copied");
System.IO.File.Copy(fileToCopy, newLocation, true);
}
else
{
MessageBox.Show("no such files");
}
}

