如何使用c#在asp.net中创建一个新的文件夹?

时间:2021-02-28 01:38:41

How can I create a new folder in asp.net using c#?

如何使用c#在asp.net中创建一个新的文件夹?

7 个解决方案

#1


19  

path is the variable holding the directory name

path是包含目录名的变量

Directory.CreateDirectory(path);

You can read more about it here

你可以在这里读到更多

#2


32  

var folder = Server.MapPath("~/App_Data/uploads/random");
if (!Directory.Exists(folder))
{
    Directory.CreateDirectory(folder);
}

#3


8  

Directory.CreateDirectory. However you will have to make sure the application pool user has rights to create the directory.

Directory.CreateDirectory。但是,您必须确保应用程序池用户拥有创建目录的权限。

#4


6  

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

try this, for a better one.

试试这个,更好的。

#5


5  

First, remember that the directory will appear on the server, not the client. You also have to have the rights to create the folder. Finally, in a load balanced environment the folder will appear only on the server that created it, it won't be replicated unless there is some background service that does that for you.

首先,请记住,目录将出现在服务器上,而不是客户端。您还必须拥有创建文件夹的权限。最后,在一个负载均衡的环境中,文件夹将只出现在创建它的服务器上,除非有后台服务为您提供此功能,否则不会被复制。

using System.IO;

Directory.CreateDirectory(folderPath);

#6


3  

Most people will say Directory.CreateDirectory(path) so I'll provide an alternative:

大多数人会说Directory.CreateDirectory(path),所以我可以提供另一种选择:

DirectoryInfo.CreateSubdirectory(name)

DirectoryInfo.CreateSubdirectory(名字)

The DirectoryInfo object will give you access to a decent amount of information about the parent directory in case there are conditions for creating the subdirectory (like checking if the parent actually exists or not). Perhaps something like:

DirectoryInfo对象将为您提供有关父目录的大量信息的访问权限,以便在有创建子目录的条件时(比如检查父目录是否确实存在)。也许类似:

var directoryInfo = new DirectoryInfo("C:\\Path\\To\\Parent\\");

if(directoryInfo.Exists)
{
    directoryInfo.CreateSubdirectory("NewFolder");
}

#7


3  

Directory.CreateDirectory(Server.MapPath(folderPath));

There is no need to check if folder exists, because if it exists method CreateDirectory does nothing.

不需要检查文件夹是否存在,因为如果它存在,CreateDirectory不会做任何事情。

#1


19  

path is the variable holding the directory name

path是包含目录名的变量

Directory.CreateDirectory(path);

You can read more about it here

你可以在这里读到更多

#2


32  

var folder = Server.MapPath("~/App_Data/uploads/random");
if (!Directory.Exists(folder))
{
    Directory.CreateDirectory(folder);
}

#3


8  

Directory.CreateDirectory. However you will have to make sure the application pool user has rights to create the directory.

Directory.CreateDirectory。但是,您必须确保应用程序池用户拥有创建目录的权限。

#4


6  

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

try this, for a better one.

试试这个,更好的。

#5


5  

First, remember that the directory will appear on the server, not the client. You also have to have the rights to create the folder. Finally, in a load balanced environment the folder will appear only on the server that created it, it won't be replicated unless there is some background service that does that for you.

首先,请记住,目录将出现在服务器上,而不是客户端。您还必须拥有创建文件夹的权限。最后,在一个负载均衡的环境中,文件夹将只出现在创建它的服务器上,除非有后台服务为您提供此功能,否则不会被复制。

using System.IO;

Directory.CreateDirectory(folderPath);

#6


3  

Most people will say Directory.CreateDirectory(path) so I'll provide an alternative:

大多数人会说Directory.CreateDirectory(path),所以我可以提供另一种选择:

DirectoryInfo.CreateSubdirectory(name)

DirectoryInfo.CreateSubdirectory(名字)

The DirectoryInfo object will give you access to a decent amount of information about the parent directory in case there are conditions for creating the subdirectory (like checking if the parent actually exists or not). Perhaps something like:

DirectoryInfo对象将为您提供有关父目录的大量信息的访问权限,以便在有创建子目录的条件时(比如检查父目录是否确实存在)。也许类似:

var directoryInfo = new DirectoryInfo("C:\\Path\\To\\Parent\\");

if(directoryInfo.Exists)
{
    directoryInfo.CreateSubdirectory("NewFolder");
}

#7


3  

Directory.CreateDirectory(Server.MapPath(folderPath));

There is no need to check if folder exists, because if it exists method CreateDirectory does nothing.

不需要检查文件夹是否存在,因为如果它存在,CreateDirectory不会做任何事情。