DirectoryInfo.Create 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
创建目录。
重载
Create() |
创建目录。 |
Create(DirectorySecurity) |
使用 DirectorySecurity 对象创建目录。 |
Create()
- Source:
- DirectoryInfo.cs
- Source:
- DirectoryInfo.cs
- Source:
- DirectoryInfo.cs
创建目录。
public:
void Create();
public void Create ();
member this.Create : unit -> unit
Public Sub Create ()
例外
无法创建目录。
示例
以下示例检查指定的目录是否存在,如果目录不存在,则创建该目录,并删除该目录。
using namespace System;
using namespace System::IO;
int main()
{
// Specify the directories you want to manipulate.
DirectoryInfo^ di = gcnew DirectoryInfo( "c:\\MyDir" );
try
{
// Determine whether the directory exists.
if ( di->Exists )
{
// Indicate that it already exists.
Console::WriteLine( "That path exists already." );
return 0;
}
// Try to create the directory.
di->Create();
Console::WriteLine( "The directory was created successfully." );
// Delete the directory.
di->Delete();
Console::WriteLine( "The directory was deleted successfully." );
}
catch ( Exception^ e )
{
Console::WriteLine( "The process failed: {0}", e );
}
}
using System;
using System.IO;
class Test
{
public static void Main()
{
// Specify the directories you want to manipulate.
DirectoryInfo di = new DirectoryInfo(@"c:\MyDir");
try
{
// Determine whether the directory exists.
if (di.Exists)
{
// Indicate that it already exists.
Console.WriteLine("That path exists already.");
return;
}
// Try to create the directory.
di.Create();
Console.WriteLine("The directory was created successfully.");
// Delete the directory.
di.Delete();
Console.WriteLine("The directory was deleted successfully.");
}
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}
finally {}
}
}
open System.IO
// Specify the directories you want to manipulate.
let di = DirectoryInfo @"c:\MyDir"
try
// Determine whether the directory exists.
if di.Exists then
// Indicate that it already exists.
printfn "That path exists already."
else
// Try to create the directory.
di.Create()
printfn "The directory was created successfully."
// Delete the directory.
di.Delete()
printfn "The directory was deleted successfully."
with e ->
printfn $"The process failed: {e}"
Imports System.IO
Public Class Test
Public Shared Sub Main()
' Specify the directories you want to manipulate.
Dim di As DirectoryInfo = New DirectoryInfo("c:\MyDir")
Try
' Determine whether the directory exists.
If di.Exists Then
' Indicate that it already exists.
Console.WriteLine("That path exists already.")
Return
End If
' Try to create the directory.
di.Create()
Console.WriteLine("The directory was created successfully.")
'Delete the directory.
di.Delete()
Console.WriteLine("The directory was deleted successfully.")
Catch e As Exception
Console.WriteLine("The process failed: {0}", e.ToString())
End Try
End Sub
End Class
注解
创建 中 path
指定的任何目录和所有目录,除非 的 path
某个部分无效。 参数 path
指定目录路径,而不是文件路径。 如果目录已存在,则此方法不执行任何操作。 如果目录在调用此方法之前不存在,则创建成功时,将刷新有关该目录的任何缓存属性信息。
有关常见 I/O 任务的列表,请参阅 常见 I/O 任务。
另请参阅
适用于
Create(DirectorySecurity)
使用 DirectorySecurity 对象创建目录。
public:
void Create(System::Security::AccessControl::DirectorySecurity ^ directorySecurity);
public void Create (System.Security.AccessControl.DirectorySecurity directorySecurity);
member this.Create : System.Security.AccessControl.DirectorySecurity -> unit
Public Sub Create (directorySecurity As DirectorySecurity)
参数
- directorySecurity
- DirectorySecurity
要应用于此目录的访问控制。
例外
调用方没有所要求的权限。
.NET Framework 和 2.1 之前的 .NET Core 版本:path
是一个零长度字符串,仅包含空格,或者包含一个或多个无效字符。 你可以使用 GetInvalidPathChars() 方法查询无效字符。
path
为 null
。
指定的路径和/或文件名超过了系统定义的最大长度。
指定的路径无效,例如位于未映射的驱动器上。
试图创建只带冒号 (:) 字符的目录。
示例
下面的代码示例使用指定的目录安全属性在用户的临时文件夹中创建一个新目录:
using System.IO;
using System.Security.AccessControl;
using System.Security.Principal;
namespace ConsoleApp
{
class Program
{
static void Main()
{
DirectorySecurity security = new DirectorySecurity();
SecurityIdentifier identity = new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null);
FileSystemAccessRule accessRule = new FileSystemAccessRule(identity, FileSystemRights.FullControl, AccessControlType.Allow);
security.AddAccessRule(accessRule);
string path = Path.Combine(Path.GetTempPath(), "directoryToCreate");
DirectoryInfo dirInfo = new DirectoryInfo(path);
dirInfo.Create(security);
}
}
}
注解
使用此方法重载创建具有访问控制的目录,因此在应用安全性之前,不可能访问该目录。
如果目录已存在,则此方法不执行任何操作。
有关常见 I/O 任务的列表,请参阅 常见 I/O 任务。
重要
此方法作为程序集的一部分System.Security.AccessControl
作为 类的FileSystemAclExtensions
扩展方法移植到 .NET Core 3.1:Create(DirectoryInfo, DirectorySecurity)。