Läs på engelska Redigera

Dela via


DirectoryInfo.Create Method

Definition

Creates a directory.

Overloads

Create()

Creates a directory.

Create(DirectorySecurity)

Creates a directory using a DirectorySecurity object.

Create()

Source:
DirectoryInfo.cs
Source:
DirectoryInfo.cs
Source:
DirectoryInfo.cs

Creates a directory.

C#
public void Create();

Exceptions

The directory cannot be created.

Examples

The following example checks whether a specified directory exists, creates the directory if it does not exist, and deletes the directory.

C#
using System;
using System.IO;

class Test
{
    public static void Main()
    {
        // Specify the directories you want to manipulate.
        DirectoryInfo di = new DirectoryInfo(@"c:\MyDir");

        try
        {
            // Determine whether the directory exists.
            if (di.Exists)
            {
                // Indicate that it already exists.
                Console.WriteLine("That path exists already.");
                return;
            }

            // Try to create the directory.
            di.Create();
            Console.WriteLine("The directory was created successfully.");

            // Delete the directory.
            di.Delete();
            Console.WriteLine("The directory was deleted successfully.");
        }
        catch (Exception e)
        {
            Console.WriteLine("The process failed: {0}", e.ToString());
        }
        finally {}
    }
}

Remarks

Any and all directories specified in path are created, unless some part of path is invalid. The path parameter specifies a directory path, not a file path. If the directory already exists, this method does nothing. If the directory did not exist before calling this method, then any cached attribute information about the directory will be flushed if the creation is successful.

For a list of common I/O tasks, see Common I/O Tasks.

See also

Applies to

.NET 10 och andra versioner
Produkt Versioner
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Create(DirectorySecurity)

Creates a directory using a DirectorySecurity object.

C#
public void Create(System.Security.AccessControl.DirectorySecurity directorySecurity);

Parameters

directorySecurity
DirectorySecurity

The access control to apply to the directory.

Exceptions

The caller does not have the required permission.

.NET Framework and .NET Core versions older than 2.1: path is a zero-length string, contains only white space, or contains one or more invalid characters. You can query for invalid characters by using the GetInvalidPathChars() method.

The specified path, file name, or both exceed the system-defined maximum length.

The specified path is invalid, such as being on an unmapped drive.

Creating a directory with only the colon (:) character was attempted.

Examples

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

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

Use this method overload to create a directory with access control, so there is no chance the directory can be accessed before security is applied.

If the directory already exists, this method does nothing.

For a list of common I/O tasks, see Common I/O Tasks.

Viktigt

This method was ported to .NET Core 3.1 as an extension method of the FileSystemAclExtensions class as part of the System.Security.AccessControl assembly: Create(DirectoryInfo, DirectorySecurity).

Applies to

.NET Framework 4.8.1 och andra versioner
Produkt Versioner
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1