如果一个文件夹不存在,创建它。

时间:2023-01-25 11:06:50

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?

我该怎么做?

14 个解决方案

#1


819  

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. The path parameter specifies a directory path, not a file path. If the directory already exists, this method does nothing.

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

#2


309  

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

#3


171  

Just write this line :

写这一行:

System.IO.Directory.CreateDirectory(myDir);
  • 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 CreateDirectory

#4


28  

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

#5


24  

Directory.Exists This will explain how to check if a FilePath Exist

目录中。这将解释如何检查FilePath是否存在。

Directory.CreateDirectory This will explain how to try and to Create the FilePath if it does not exist

目录中。CreateDirectory将解释如何尝试创建FilePath,如果它不存在的话。

#6


15  

using System.IO

if (!Directory.Exists(yourDirectory))
    Directory.CreateDirectory(yourDirectory);

#7


12  

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

#8


11  

This method will create folder if not exist and do nothing if exists

此方法将创建文件夹,如果不存在,则不执行任何操作。

Directory.CreateDirectory(path);

#9


11  

if (!Directory.Exists(Path.GetDirectoryName(fileName)))
{
    Directory.CreateDirectory(Path.GetDirectoryName(fileName));
}

#10


4  

Following code is the best line(s) of code i use that will create directory if not present .

下面的代码是我使用的最好的代码行,如果不存在,它将创建目录。

System.IO.Directory.CreateDirectory(Server.MapPath("~/temp/"));

If the directory already exists, this method does not create a new directory, but it returns a DirectoryInfo object for the existing directory. >

如果目录已经存在,此方法不会创建新目录,但它将返回现有目录的DirectoryInfo对象。>

#11


1  

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");

   }
}

#12


0  

string createfolder = "E:/tmp/" + uId;
System.IO.Directory.CreateDirectory(createfolder);

string createfolder = "E:/tmp/" + uId;System.IO.Directory.CreateDirectory(createfolder);

#13


-1  

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目录中创建了一个Mahesh子目录。

// Create sub directory

if (!Directory.Exists(subdir))
{

Directory.CreateDirectory(subdir);

}

#14


-2  

Derived/combined from multiple answers, implementing it for me was as easy as this:

从多个答案中导出/组合,为我实现它就像这样简单:

public void Init()
{
    String platypusDir = @"C:\platypus";
    CreateDirectoryIfDoesNotExist(platypusDir);
}

private void CreateDirectoryIfDoesNotExist(string dirName)
{
    System.IO.Directory.CreateDirectory(dirName);
}

#1


819  

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. The path parameter specifies a directory path, not a file path. If the directory already exists, this method does nothing.

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

#2


309  

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

#3


171  

Just write this line :

写这一行:

System.IO.Directory.CreateDirectory(myDir);
  • 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 CreateDirectory

#4


28  

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

#5


24  

Directory.Exists This will explain how to check if a FilePath Exist

目录中。这将解释如何检查FilePath是否存在。

Directory.CreateDirectory This will explain how to try and to Create the FilePath if it does not exist

目录中。CreateDirectory将解释如何尝试创建FilePath,如果它不存在的话。

#6


15  

using System.IO

if (!Directory.Exists(yourDirectory))
    Directory.CreateDirectory(yourDirectory);

#7


12  

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

#8


11  

This method will create folder if not exist and do nothing if exists

此方法将创建文件夹,如果不存在,则不执行任何操作。

Directory.CreateDirectory(path);

#9


11  

if (!Directory.Exists(Path.GetDirectoryName(fileName)))
{
    Directory.CreateDirectory(Path.GetDirectoryName(fileName));
}

#10


4  

Following code is the best line(s) of code i use that will create directory if not present .

下面的代码是我使用的最好的代码行,如果不存在,它将创建目录。

System.IO.Directory.CreateDirectory(Server.MapPath("~/temp/"));

If the directory already exists, this method does not create a new directory, but it returns a DirectoryInfo object for the existing directory. >

如果目录已经存在,此方法不会创建新目录,但它将返回现有目录的DirectoryInfo对象。>

#11


1  

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");

   }
}

#12


0  

string createfolder = "E:/tmp/" + uId;
System.IO.Directory.CreateDirectory(createfolder);

string createfolder = "E:/tmp/" + uId;System.IO.Directory.CreateDirectory(createfolder);

#13


-1  

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目录中创建了一个Mahesh子目录。

// Create sub directory

if (!Directory.Exists(subdir))
{

Directory.CreateDirectory(subdir);

}

#14


-2  

Derived/combined from multiple answers, implementing it for me was as easy as this:

从多个答案中导出/组合,为我实现它就像这样简单:

public void Init()
{
    String platypusDir = @"C:\platypus";
    CreateDirectoryIfDoesNotExist(platypusDir);
}

private void CreateDirectoryIfDoesNotExist(string dirName)
{
    System.IO.Directory.CreateDirectory(dirName);
}