共用方式為


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,以帶入下列專案所提供的功能:

適用於