Muokkaa

Jaa


FileSystemAclExtensions.Create Method

Definition

Overloads

Create(DirectoryInfo, DirectorySecurity)

Creates a new directory, ensuring it is created with the specified directory security. If the directory already exists, nothing is done.

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

Creates a new file stream, ensuring it is created with the specified properties and security settings.

Create(DirectoryInfo, DirectorySecurity)

Creates a new directory, ensuring it is created with the specified directory security. If the directory already exists, nothing is done.

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)

Parameters

directoryInfo
DirectoryInfo

A directory that does not exist yet that will be created by the method.

directorySecurity
DirectorySecurity

The access control and audit security for the directory.

Exceptions

directoryInfo or directorySecurity is null.

Could not find a part of the path.

Access to the path is denied.

Examples

The following code example creates a new directory inside the user's temporary folder with the specified directory security attributes:

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

Remarks

This extension method was added to .NET Core to bring the functionality that was provided by the DirectoryInfo.Create(DirectorySecurity) .NET Framework method.

Applies to

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

Creates a new file stream, ensuring it is created with the specified properties and security settings.

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

Parameters

fileInfo
FileInfo

A file that does not exist yet that will be created by the method.

mode
FileMode

One of the enumeration values that specifies how the operating system should open a file.

rights
FileSystemRights

One of the enumeration values that defines the access rights to use when creating access and audit rules.

share
FileShare

One of the enumeration values for controlling the kind of access other file stream objects can have to the same file.

bufferSize
Int32

The number of bytes buffered for reads and writes to the file.

options
FileOptions

One of the enumeration values that describes how to create or overwrite the file.

fileSecurity
FileSecurity

An object that determines the access control and audit security for the file.

Returns

A file stream for the newly created file.

Exceptions

The rights and mode combination is invalid.

fileInfo or fileSecurity is null.

mode or share are out of their legal enum range.

-or-

bufferSize is not a positive number.

Could not find a part of the path.

An I/O error occurred.

Access to the path is denied.

Examples

The following code example creates a new text file inside the user's temporary folder, explicitly specifying all security attributes:

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
        }
    }
}

Remarks

This extension method was added to .NET Core to bring the functionality that was provided by:

Applies to