FileSystemAclExtensions.Create 方法

定义

重载

Create(DirectoryInfo, DirectorySecurity)

创建一个新目录,确保使用指定的目录安全性创建该目录。 如果该目录已存在,则不执行任何操作。

Create(FileInfo, FileMode, FileSystemRights, FileShare, Int32, FileOptions, FileSecurity)

创建一个新的文件流,确保使用指定的属性和安全设置创建该文件流。

Create(DirectoryInfo, DirectorySecurity)

创建一个新目录,确保使用指定的目录安全性创建该目录。 如果该目录已存在,则不执行任何操作。

public:
[System::Runtime::CompilerServices::Extension]
 static void Create(System::IO::DirectoryInfo ^ directoryInfo, System::Security::AccessControl::DirectorySecurity ^ directorySecurity);
public static void Create (this System.IO.DirectoryInfo directoryInfo, System.Security.AccessControl.DirectorySecurity directorySecurity);
static member Create : System.IO.DirectoryInfo * System.Security.AccessControl.DirectorySecurity -> unit
<Extension()>
Public Sub Create (directoryInfo As DirectoryInfo, directorySecurity As DirectorySecurity)

参数

directoryInfo
DirectoryInfo

一个尚不存在但将通过此方法创建的目录。

directorySecurity
DirectorySecurity

目录的访问控制和审核安全。

例外

directoryInfodirectorySecuritynull

找不到路径的某一部分。

对路径的访问被拒绝。

示例

下面的代码示例使用指定的目录安全属性在用户的临时文件夹中创建一个新目录:

using System.IO;
using System.Runtime.Versioning;
using System.Security.AccessControl;
using System.Security.Principal;

namespace MyNamespace
{
    public class MyClassCS
    {
        // Attribute to address CA1416 warning:
        // System.IO.FileSystem.AccessControl APIs are  only available on Windows
        [SupportedOSPlatform("windows")]
        static void Main()
        {
            // Create the file security object

            SecurityIdentifier identity = new SecurityIdentifier(
                WellKnownSidType.BuiltinUsersSid, // This maps to "Everyone" user group in Windows
                null); // null is OK for this particular user group. For others, a non-empty value might be required
            FileSystemAccessRule accessRule = new FileSystemAccessRule(identity, FileSystemRights.FullControl, AccessControlType.Allow);

            DirectorySecurity expectedSecurity = new DirectorySecurity();
            expectedSecurity.AddAccessRule(accessRule);

            // Make sure the directory does not exist, then create it

            string dirPath = Path.Combine(Path.GetTempPath(), "directoryToCreate");
            DirectoryInfo dirInfo = new DirectoryInfo(dirPath);
            if (dirInfo.Exists)
            {
                dirInfo.Delete(recursive: true);
            }

            dirInfo.Create(expectedSecurity);
        }
    }
}

注解

此扩展方法已添加到 .NET Core,使 DirectoryInfo.Create (DirectorySecurity) .NET Framework 方法提供的功能。

适用于

Create(FileInfo, FileMode, FileSystemRights, FileShare, Int32, FileOptions, FileSecurity)

创建一个新的文件流,确保使用指定的属性和安全设置创建该文件流。

public:
[System::Runtime::CompilerServices::Extension]
 static System::IO::FileStream ^ Create(System::IO::FileInfo ^ fileInfo, System::IO::FileMode mode, System::Security::AccessControl::FileSystemRights rights, System::IO::FileShare share, int bufferSize, System::IO::FileOptions options, System::Security::AccessControl::FileSecurity ^ fileSecurity);
public static System.IO.FileStream Create (this System.IO.FileInfo fileInfo, System.IO.FileMode mode, System.Security.AccessControl.FileSystemRights rights, System.IO.FileShare share, int bufferSize, System.IO.FileOptions options, System.Security.AccessControl.FileSecurity fileSecurity);
public static System.IO.FileStream Create (this System.IO.FileInfo fileInfo, System.IO.FileMode mode, System.Security.AccessControl.FileSystemRights rights, System.IO.FileShare share, int bufferSize, System.IO.FileOptions options, System.Security.AccessControl.FileSecurity? fileSecurity);
static member Create : System.IO.FileInfo * System.IO.FileMode * System.Security.AccessControl.FileSystemRights * System.IO.FileShare * int * System.IO.FileOptions * System.Security.AccessControl.FileSecurity -> System.IO.FileStream
<Extension()>
Public Function Create (fileInfo As FileInfo, mode As FileMode, rights As FileSystemRights, share As FileShare, bufferSize As Integer, options As FileOptions, fileSecurity As FileSecurity) As FileStream

参数

fileInfo
FileInfo

一个尚不存在但将通过此方法创建的文件。

mode
FileMode

一个枚举值,用于指定操作系统应如何打开文件。

rights
FileSystemRights

一个枚举值,用于定义在创建访问和审核规则时要使用的访问权限。

share
FileShare

一个枚举值,用于控制其他文件流对象可对同一文件进行哪种类型的访问。

bufferSize
Int32

用于读取和写入到文件的已放入缓冲区的字节数。

options
FileOptions

一个枚举值,用于描述如何创建或覆盖该文件。

fileSecurity
FileSecurity

一个对象,用于确定文件的访问控制和审核安全性。

返回

新建文件的文件流。

例外

rightsmode 组合无效。

fileInfofileSecuritynull

modeshare 超出其合法的枚举范围。

- 或 -

bufferSize 不是正数。

找不到路径的某一部分。

出现 I/O 错误。

对路径的访问被拒绝。

示例

下面的代码示例在用户的临时文件夹中创建新的文本文件,显式指定所有安全属性:

using System;
using System.IO;
using System.Runtime.Versioning;
using System.Security.AccessControl;
using System.Security.Principal;
using System.Text;

namespace MyNamespace
{
    public class MyClassCS
    {
        // Attribute to address CA1416 warning:
        // System.IO.FileSystem.AccessControl APIs are  only available on Windows
        [SupportedOSPlatform("windows")]
        static void Main()
        {
            // Create the file security object

            var identity = new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null);
            var accessRule = new FileSystemAccessRule(identity, FileSystemRights.FullControl, AccessControlType.Allow);

            var security = new FileSecurity();
            security.AddAccessRule(accessRule);

            // Make sure the file does not exist, or FileMode.CreateNew will throw

            string filePath = Path.Combine(Path.GetTempPath(), "temp.txt");
            var fileInfo = new FileInfo(filePath);
            if (fileInfo.Exists)
            {
                fileInfo.Delete();
            }

            // Create the file with the specified security and write some text

            using (FileStream stream = fileInfo.Create(
                FileMode.CreateNew,
                FileSystemRights.FullControl,
                FileShare.ReadWrite,
                4096, // Default buffer size
                FileOptions.None,
                security))
            {
                string text = "Hello world!";
                byte[] writeBuffer = new UTF8Encoding(encoderShouldEmitUTF8Identifier: true).GetBytes(text);

                stream.Write(writeBuffer, 0, writeBuffer.Length);
            } // Dispose flushes the file to disk
        }
    }
}

注解

此扩展方法已添加到 .NET Core 中,以带来以下提供的功能:

适用于