英語で読む

次の方法で共有


FileSystemAclExtensions.Create メソッド

定義

オーバーロード

Create(DirectoryInfo, DirectorySecurity)

新しいファイル ストリームを作成します。作成時は、指定されたディレクトリ セキュリティを使用していることを確認します。 ディレクトリが既に存在する場合は、何も実行されません。

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

新しいファイル ストリームを作成します。作成時は、指定されたプロパティおよびセキュリティ設定を使用していることを確認します。

Create(DirectoryInfo, DirectorySecurity)

新しいファイル ストリームを作成します。作成時は、指定されたディレクトリ セキュリティを使用していることを確認します。 ディレクトリが既に存在する場合は、何も実行されません。

C#
public static void Create (this System.IO.DirectoryInfo directoryInfo, System.Security.AccessControl.DirectorySecurity directorySecurity);

パラメーター

directoryInfo
DirectoryInfo

メソッドによって作成される、まだ存在しないディレクトリ。

directorySecurity
DirectorySecurity

ディレクトリのアクセス制御と監査セキュリティ。

例外

directoryInfo または directorySecuritynull です。

パスの一部が見つかりませんでした。

パスへのアクセスが拒否されました。

次のコード例では、指定したディレクトリ セキュリティ属性を使用して、ユーザーの一時フォルダー内に新しいディレクトリを作成します。

C#
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);
        }
    }
}

注釈

この拡張メソッドは、DirectoryInfo.Create によって提供された機能を提供するために.NET Core に追加されました(DirectorySecurity).NET Framework メソッド。

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET 6, 7, 8, 9
Windows Desktop 3.1, 5

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

新しいファイル ストリームを作成します。作成時は、指定されたプロパティおよびセキュリティ設定を使用していることを確認します。

C#
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);
C#
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);

パラメーター

fileInfo
FileInfo

メソッドによって作成される、まだ存在しないファイル。

mode
FileMode

オペレーティング システムでどのようにファイルを開くかを指定する列挙値の 1 つ。

rights
FileSystemRights

アクセス規則および監査規則を作成する際に使用するアクセス権を定義する列挙値の 1 つ。

share
FileShare

他のファイル ストリーム オブジェクトが持つことのできる、同じファイルへのアクセス権の種類を制御するための列挙値の 1 つ。

bufferSize
Int32

ファイルの読み取りおよび書き込み用にバッファリングされるバイト数。

options
FileOptions

ファイルを作成または上書きする方法を示す列挙値のいずれか。

fileSecurity
FileSecurity

ファイルのアクセス制御と監査セキュリティを決定するオブジェクト。

戻り値

新しく作成されたファイルのファイル ストリームです。

例外

rightsmode の組み合わせが無効です。

fileInfo または fileSecuritynull です。

mode または share が列挙型の有効な範囲を超えています。

- または -

bufferSize が正の数ではありません。

パスの一部が見つかりませんでした。

I/O エラーが発生しました。

パスへのアクセスが拒否されました。

次のコード例では、ユーザーの一時フォルダー内に新しいテキスト ファイルを作成し、すべてのセキュリティ属性を明示的に指定します。

C#
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 に追加され、以下によって提供された機能が提供されました。

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET 6, 7, 8, 9
Windows Desktop 3.1, 5