Export (0) Print
Expand All

Directory.Delete Method (String)

.NET Framework 1.1

Deletes an empty directory from a specified path.

[Visual Basic]
Overloads Public Shared Sub Delete( _
   ByVal path As String _
)
[C#]
public static void Delete(
 string path
);
[C++]
public: static void Delete(
 String* path
);
[JScript]
public static function Delete(
   path : String
);

Parameters

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

Exceptions

Exception Type Condition
IOException The directory specified by path is read-only or is not empty.
UnauthorizedAccessException The caller does not have the required permission.
ArgumentException path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by InvalidPathChars.
ArgumentNullException path is a null reference (Nothing in Visual Basic).
PathTooLongException The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
DirectoryNotFoundException The specified path is invalid, such as being on an unmapped drive.

Remarks

This method behaves identically to Delete(String, Boolean) with false specified.

The path parameter is permitted to 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.

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

The path parameter is not case-sensitive.

For an example of using this method, see the Example section below. The following table lists examples of other typical or related I/O tasks.

To do this... See the example in this topic...
Copy a directory. Directory
Rename or move a directory. Directory.Move

DirectoryInfo.MoveTo

Delete a file. File.Delete

FileInfo.Delete

Create a directory. CreateDirectory

Directory

Create a subdirectory. CreateSubdirectory
See the files in a directory. Name
See the subdirectories of a directory. GetDirectories

GetDirectories

See all the files in all subdirectories of a directory. GetFileSystemInfos

.NET Compact Framework Platform Note:  Some device file systems do not support relative paths. Specify absolute path information.

Example

[Visual Basic, C#, C++] The following example deletes the specified directory or throws an exception if there are subdirectories.

[Visual Basic] 
Imports System
Imports System.IO

Public Class Test

    Public Shared Sub Main()
        ' Specify the directories you want to manipulate.
        Dim path As String = "c:\MyDir"
        Dim subPath As String = "c:\MyDir\temp"

        Try
            ' Determine whether the directory exists.
            If Directory.Exists(path) = False Then
                ' Create the directory if it does not exist.
                Directory.CreateDirectory(path)
            End If

            ' Determine whether the subdirectory exists.
            If Directory.Exists(subPath) = False Then
                ' Create the subdirectory if it does not exist.
                Directory.CreateDirectory(SubPath)
            End If

            ' This Delete operation will not be allowed because there are subdirectories.
            Console.WriteLine("I am about to attempt to delete {0}.", path)
            Directory.Delete(path)
            Console.WriteLine("The Delete operation was successful, which was unexpected.")

        Catch
            Console.WriteLine("The Delete operation failed as expected.")
        End Try
    End Sub
End Class

[C#] 
using System;
using System.IO;

class Test 
{
    public static void Main() 
    {
        // Specify the directories you want to manipulate.
        string path = @"c:\MyDir";
        string subPath = @"c:\MyDir\temp";

        try 
        {
            // Determine whether the directory exists.
            if (!Directory.Exists(path)) 
            {
                // Create the directory if it does not exist.
                Directory.CreateDirectory(path);
            }


            if (!Directory.Exists(subPath)) 
            {
                // Create the subdirectory if it does not exist.
                Directory.CreateDirectory(subPath);
            }

            // This operation will not be allowed because there are subdirectories.
            Console.WriteLine("I am about to attempt to delete {0}.", path);
            Directory.Delete(path);
            Console.WriteLine("The Delete operation was successful, which was unexpected.");

        } 
        catch (Exception) 
        {
            Console.WriteLine("The Delete operation failed as expected.");
        } 
        finally {}
    }
}

[C++] 
#using <mscorlib.dll>

using namespace System;
using namespace System::IO;

int main() {
    // Specify the directories you want to manipulate.
    String* path = S"c:\\MyDir";
    String* subPath = S"c:\\MyDir\\temp";

    try {
        // Determine whether the directory exists.
        if (!Directory::Exists(path)) {
            // Create the directory if it does not exist.
            Directory::CreateDirectory(path);
        }

        if (!Directory::Exists(subPath)) {
            // Create the subdirectory if it does not exist.
            Directory::CreateDirectory(subPath);
        }

        // This operation will not be allowed because there are subdirectories.
        Console::WriteLine(S"I am about to attempt to delete {0}.", path);
        Directory::Delete(path);
        Console::WriteLine(S"The Delete operation was successful, which was unexpected.");

    } catch (Exception*) {
        Console::WriteLine(S"The Delete operation failed as expected.");
    } 
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

.NET Framework Security: 

See Also

Directory Class | Directory Members | System.IO Namespace | Directory.Delete Overload List | Working with I/O | Reading Text from a File | Writing Text to a File

Show:
© 2015 Microsoft