語言

FileSystemAclExtensions.Create 方法

定義

多載

名稱 Description
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

目錄的存取控制與審計安全。

例外狀況

directoryInfo 或 directorySecurity 為 null。

找不到路徑的一部分。

通往該步道的通道被拒絕。

範例

以下程式碼範例在使用者的暫存資料夾中建立一個新目錄,並設定指定的目錄安全屬性:

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

對象,決定檔案的訪問控制和稽核安全性。

傳回

一個用於新建立檔案的檔案串流。

例外狀況

rights與mode組合是無效的。

fileInfo 或 fileSecurity 為 null。

mode 或 share 超出法定的門戶範圍。

-或-

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 核心,以帶來以下功能:

適用於