संपादित करें

इसके माध्यम से साझा किया गया


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.

public:
 static void Delete(System::String ^ path);
public static void Delete (string path);
static member Delete : string -> unit
Public Shared Sub Delete (path As String)

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.

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);
            }
        }
    }
}
open System.IO

let subPath = @"C:\NewDirectory\NewSubDirectory"

try
    Directory.CreateDirectory subPath |> ignore
    Directory.Delete subPath

    let directoryExists = Directory.Exists @"C:\NewDirectory"
    let subDirectoryExists = Directory.Exists subPath

    printfn $"top-level directory exists: {directoryExists}"
    printfn $"sub-directory exists: {subDirectoryExists}"
with e ->
    printfn $"The process failed: {e.Message}"
Imports System.IO

Module Module1

    Sub Main()
        Dim subPath = "C:\NewDirectory\NewSubDirectory"

        Try
            Directory.CreateDirectory(subPath)
            Directory.Delete(subPath)

            Dim directoryExists = Directory.Exists("C:\NewDirectory")
            Dim subDirectoryExists = Directory.Exists(subPath)

            Console.WriteLine("top-level directory exists: " & directoryExists)
            Console.WriteLine("sub-directory exists: " & subDirectoryExists)

        Catch e As Exception
            Console.WriteLine("The process failed: {0}", e.Message)
        End Try
    End Sub

End Module

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

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.

public:
 static void Delete(System::String ^ path, bool recursive);
public static void Delete (string path, bool recursive);
static member Delete : string * bool -> unit
Public Shared Sub Delete (path As String, recursive As Boolean)

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.

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);
            }
        }
    }
}
open System.IO

let topPath = @"C:\NewDirectory"
let subPath = @"C:\NewDirectory\NewSubDirectory"

try
    Directory.CreateDirectory(subPath) |> ignore

    do
        use writer = File.CreateText(subPath + @"\example.txt")
        writer.WriteLine "content added"

    Directory.Delete(topPath, true)

    let directoryExists = Directory.Exists topPath

    printfn $"top-level directory exists: {directoryExists}"
    
with e ->
    printfn $"The process failed: {e.Message}"
Imports System.IO

Module Module1

    Sub Main()
        Dim topPath = "C:\NewDirectory"
        Dim subPath = "C:\NewDirectory\NewSubDirectory"

        Try
            Directory.CreateDirectory(subPath)

            Using writer As StreamWriter = File.CreateText(subPath + "\example.txt")
                writer.WriteLine("content added")
            End Using

            Directory.Delete(topPath, True)

            Dim directoryExists = Directory.Exists(topPath)

            Console.WriteLine("top-level directory exists: " & directoryExists)
        Catch e As Exception
            Console.WriteLine("The process failed: {0}", e.Message)
        End Try
    End Sub

End Module

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