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.Security.AccessControl;
using System.Security.Principal;

namespace ConsoleApp
{
    class Program
    {
        static void Main()
        {
            DirectorySecurity security = new DirectorySecurity();

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

            string path = Path.Combine(Path.GetTempPath(), "directoryToCreate");

            DirectoryInfo dirInfo = new DirectoryInfo(path);

            dirInfo.Create(security);
        }
    }
}

Remarks

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

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

FileStream

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 (with the default buffer size of 4096) inside the user's temporary folder with the specified file security attributes:

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

namespace ConsoleApp
{
    class Program
    {
        static void Main()
        {
            FileSecurity security = new FileSecurity();

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

            string path = Path.Combine(Path.GetTempPath(), "fileToCreate.txt");

            FileInfo fileInfo = new FileInfo(path);

            fileInfo.Create(FileMode.Create, FileSystemRights.FullControl, FileShare.Read, 4096, FileOptions.None, security);
        }
    }
}

Remarks

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

Applies to