FileSystemAclExtensions.Create Metod

Definition

Överlagringar

Name Description
Create(DirectoryInfo, DirectorySecurity)

Skapar en ny katalog, vilket säkerställer att den skapas med den angivna katalogsäkerheten. Om katalogen redan finns görs ingenting.

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

Skapar en ny filström, vilket säkerställer att den skapas med de angivna egenskaperna och säkerhetsinställningarna.

Create(DirectoryInfo, DirectorySecurity)

Skapar en ny katalog, vilket säkerställer att den skapas med den angivna katalogsäkerheten. Om katalogen redan finns görs ingenting.

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)

Parametrar

directoryInfo
DirectoryInfo

En katalog som inte finns ännu och som skapas med metoden .

directorySecurity
DirectorySecurity

Åtkomstkontroll och granskningssäkerhet för katalogen.

Undantag

directoryInfo eller directorySecurity är null.

Det gick inte att hitta en del av sökvägen.

Åtkomst till sökvägen nekas.

Exempel

I följande kodexempel skapas en ny katalog i användarens temporära mapp med de angivna katalogsäkerhetsattributen:

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

Kommentarer

Den här tilläggsmetoden har lagts till i .NET Core för att ta med de funktioner som tillhandahölls av metoden DirectoryInfo.Create(DirectorySecurity) .NET Framework.

Gäller för

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

Skapar en ny filström, vilket säkerställer att den skapas med de angivna egenskaperna och säkerhetsinställningarna.

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

Parametrar

fileInfo
FileInfo

En fil som inte finns ännu och som skapas med metoden .

mode
FileMode

Ett av uppräkningsvärdena som anger hur operativsystemet ska öppna en fil.

rights
FileSystemRights

Ett av uppräkningsvärdena som definierar de åtkomsträttigheter som ska användas när du skapar åtkomst- och granskningsregler.

share
FileShare

Ett av uppräkningsvärdena för att styra vilken typ av åtkomst andra filströmsobjekt kan ha till samma fil.

bufferSize
Int32

Antalet byte som buffrats för läsningar och skrivningar till filen.

options
FileOptions

Ett av uppräkningsvärdena som beskriver hur du skapar eller skriver över filen.

fileSecurity
FileSecurity

Ett objekt som avgör åtkomstkontroll och granskningssäkerhet för filen.

Returer

En filström för den nyligen skapade filen.

Undantag

Kombinationen rights och mode är ogiltig.

fileInfo eller fileSecurity är null.

mode eller share ligger inom det juridiska uppräkningsintervallet.

-eller-

bufferSize är inte ett positivt tal.

Det gick inte att hitta en del av sökvägen.

Ett I/O-fel uppstod.

Åtkomst till sökvägen nekas.

Exempel

I följande kodexempel skapas en ny textfil i användarens temporära mapp, som uttryckligen anger alla säkerhetsattribut:

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

Kommentarer

Den här tilläggsmetoden lades till i .NET Core för att få de funktioner som tillhandahölls av:

Gäller för