Läs på engelska Redigera

Dela via


Directory.Delete Method

Definition

Deletes a specified directory, and optionally any subdirectories.

Overloads

Delete(String)

Deletes an empty directory from a specified path.

Delete(String, Boolean)

Deletes the specified directory and, if indicated, any subdirectories and files in the directory.

Delete(String)

Source:
Directory.cs
Source:
Directory.cs
Source:
Directory.cs

Deletes an empty directory from a specified path.

C#
public static void Delete(string path);

Parameters

path
String

The name of the empty directory to remove. This directory must be writable and empty.

Exceptions

A file with the same name and location specified by path exists.

-or-

The directory is the application's current working directory.

-or-

The directory specified by path is not empty.

-or-

The directory is read-only or contains a read-only file.

-or-

The directory is being used by another process.

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.

path is null.

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

path does not exist or could not be found.

-or-

The specified path is invalid (for example, it is on an unmapped drive).

Examples

The following example shows how to create a new directory and subdirectory, and then delete only the subdirectory.

C#
using System;
using System.IO;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string subPath = @"C:\NewDirectory\NewSubDirectory";

            try
            {
                Directory.CreateDirectory(subPath);
                Directory.Delete(subPath);

                bool directoryExists = Directory.Exists(@"C:\NewDirectory");
                bool subDirectoryExists = Directory.Exists(subPath);

                Console.WriteLine("top-level directory exists: " + directoryExists);
                Console.WriteLine("sub-directory exists: " + subDirectoryExists);
            }
            catch (Exception e)
            {
                Console.WriteLine("The process failed: {0}", e.Message);
            }
        }
    }
}

Remarks

This method behaves identically to Delete(String, Boolean) with false specified for the second parameter.

The path parameter may specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see GetCurrentDirectory.

Trailing spaces are removed from the end of the path parameter before deleting the directory.

This method throws an IOException if the directory specified in the path parameter contains files or subdirectories.

The case-sensitivity of the path parameter corresponds to that of the file system on which the code is running. For example, it's case-insensitive on NTFS (the default Windows file system) and case-sensitive on Linux file systems.

In some cases, if you have the specified directory open in File Explorer, the Delete method may not be able to delete it.

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

Delete(String, Boolean)

Source:
Directory.cs
Source:
Directory.cs
Source:
Directory.cs

Deletes the specified directory and, if indicated, any subdirectories and files in the directory.

C#
public static void Delete(string path, bool recursive);

Parameters

path
String

The name of the directory to remove.

recursive
Boolean

true to remove directories, subdirectories, and files in path; otherwise, false.

Exceptions

A file with the same name and location specified by path exists.

-or-

The directory specified by path is read-only, or recursive is false and path is not an empty directory.

-or-

The directory is the application's current working directory.

-or-

The directory contains a read-only file.

-or-

The directory is being used by another process.

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.

path is null.

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

path does not exist or could not be found.

-or-

The specified path is invalid (for example, it is on an unmapped drive).

Examples

The following example shows how to create a new directory, subdirectory, and file in the subdirectory, and then recursively delete all the new items.

C#
using System;
using System.IO;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string topPath = @"C:\NewDirectory";
            string subPath = @"C:\NewDirectory\NewSubDirectory";

            try
            {
                Directory.CreateDirectory(subPath);

                using (StreamWriter writer = File.CreateText(subPath + @"\example.txt"))
                {
                    writer.WriteLine("content added");
                }

                Directory.Delete(topPath, true);

                bool directoryExists = Directory.Exists(topPath);

                Console.WriteLine("top-level directory exists: " + directoryExists);
            }
            catch (Exception e)
            {
                Console.WriteLine("The process failed: {0}", e.Message);
            }
        }
    }
}

Remarks

The path parameter may specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see GetCurrentDirectory.

Trailing spaces are removed from the end of the path parameter before deleting the directory.

The case-sensitivity of the path parameter corresponds to that of the file system on which the code is running. For example, it's case-insensitive on NTFS (the default Windows file system) and case-sensitive on Linux file systems.

If the recursive parameter is true, the user must have write permission for the current directory as well as for all subdirectories.

The behavior of this method differs slightly when deleting a directory that contains a reparse point, such as a symbolic link or a mount point. If the reparse point is a directory, such as a mount point, it is unmounted and the mount point is deleted. This method does not recurse through the reparse point. If the reparse point is a symbolic link to a file, the reparse point is deleted and not the target of the symbolic link.

In some cases, if you have the specified directory open in File Explorer, the Delete method may not be able to delete it.

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