Export (0) Print
Expand All

Directory.Delete Method (String)

Deletes an empty directory from a specified path.

Namespace: System.IO
Assembly: mscorlib (in mscorlib.dll)

public static void Delete (
	string path
)
public static void Delete (
	String path
)
public static function Delete (
	path : String
)

Parameters

path

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

Exception typeCondition

IOException

A file with the same name and location specifiedby path exists.

-or-

The directory is the application's current working directory.

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 (for example, it is on an unmapped drive).

This method behaves identically to Delete 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.

The following table lists examples of other typical or related I/O tasks.

To do this...

See the example in this topic...

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

Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows CE Platform Note: Some device file systems do not support relative paths. Specify absolute path information.

The following code example deletes the specified directory or throws an exception if there are subdirectories.

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

import System.*;
import System.IO.*;

class Test
{
    public static void main(String[] args)
    {
        // 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 (System.Exception exp) {
            Console.WriteLine("The Delete operation failed as expected.");
        }
        finally {
        }
    } //main
} //Test

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft