Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

IsolatedStorageFile.DeleteDirectory Method

Deletes a directory in the isolated storage scope.

[Visual Basic]
Public Sub DeleteDirectory( _
   ByVal dir As String _
)
[C#]
public void DeleteDirectory(
 string dir
);
[C++]
public: void DeleteDirectory(
 String* dir
);
[JScript]
public function DeleteDirectory(
   dir : String
);

Parameters

dir
The relative path of the directory to delete within the isolated storage scope.

Exceptions

Exception Type Condition
IsolatedStorageException The directory could not be deleted.
ArgumentNullException The directory path was a null reference (Nothing in Visual Basic).

Remarks

A directory must be empty before it is deleted. The deleted directory cannot be recovered once deleted.

The Deleting Files and Directories example demonstrates the use of the DeleteDirectory method.

Example

[Visual Basic] 
' This method deletes directories in the specified Isolated Storage, after first 
' deleting the files they contain. In this example, the Archive directory is deleted. 
' There should be no other directories in this Isolated Storage.
Public Sub DeleteDirectories()
    Try
        Dim isoFile As IsolatedStorageFile = IsolatedStorageFile.GetStore(IsolatedStorageScope.User _
            Or IsolatedStorageScope.Assembly Or IsolatedStorageScope.Domain, _
            GetType(System.Security.Policy.Url), GetType(System.Security.Policy.Url))
        Dim name As String
        Dim dirNames As [String]() = isoFile.GetDirectoryNames("*")
        Dim fileNames As [String]() = isoFile.GetFileNames("Archive\*")
        ' Delete all the files currently in the Archive directory.
        If fileNames.Length > 0 Then
            For Each name In fileNames
                isoFile.DeleteFile(("Archive\" & name))
            Next name
            'Confirm no files are left.
            fileNames = isoFile.GetFileNames("Archive\*")
        End If
        If dirNames.Length > 0 Then
            For Each name In dirNames
                ' Delete the Archive directory.
                isoFile.DeleteDirectory(name)
            Next name
        End If
        dirNames = isoFile.GetDirectoryNames("*")
        isoFile.Remove()
    Catch ex As Exception
        Console.WriteLine(ex.ToString())
    End Try
End Sub 'DeleteDirectories


[C#] 
// This method deletes directories in the specified Isolated Storage, after first 
// deleting the files they contain. In this example, the Archive directory is deleted. 
// There should be no other directories in this Isolated Storage.
    public void DeleteDirectories()
    {
        try
        {
            IsolatedStorageFile isoFile = IsolatedStorageFile.GetStore( IsolatedStorageScope.User |
                IsolatedStorageScope.Assembly |
                IsolatedStorageScope.Domain,
                typeof(System.Security.Policy.Url),
                typeof(System.Security.Policy.Url));
            String [] dirNames  = isoFile.GetDirectoryNames("*");
            String [] fileNames = isoFile.GetFileNames("Archive\\*");

            // Delete all the files currently in the Archive directory.

            if (fileNames.Length>0)
            {
                for (int i=0;i<fileNames.Length;++i) 
                {
                    // Delete the files.
                    isoFile.DeleteFile("Archive\\" + fileNames[i]);
                }
                // Confirm that no files remain.
                fileNames = isoFile.GetFileNames("Archive\\*");
            }


            if (dirNames.Length>0)
            {
                for (int i=0; i<dirNames.Length; ++i) 
                {
                    // Delete the Archive directory.
                }
            }
            dirNames = isoFile.GetDirectoryNames("*");
            isoFile.Remove();
        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }
    }

[C++] 
// This method deletes directories in the specified Isolated Storage, after first 
// deleting the files they contain. In this example, the Archive directory is deleted. 
// There should be no other directories in this Isolated Storage.
void DeleteDirectories()
{
   try
   {
      IsolatedStorageFile* isoFile = IsolatedStorageFile::GetStore(
         static_cast<IsolatedStorageScope>(IsolatedStorageScope::User |
         IsolatedStorageScope::Assembly |
         IsolatedStorageScope::Domain),
         __typeof(System::Security::Policy::Url),
         __typeof(System::Security::Policy::Url));

      String* dirNames[]  = isoFile->GetDirectoryNames(S"*");
      String* fileNames[] = isoFile->GetFileNames(S"Archive\\*");

      // Delete the current files within the Archive directory.

      if (fileNames->Length>0)
      {
         for (int i=0;i<fileNames->Length;++i) 
         {
            //delete files
            isoFile->DeleteFile((S"Archive\\{0}", fileNames[i]));
         }
         //Confirm no files are left.
         fileNames = isoFile->GetFileNames(S"Archive\\*");
      }


      if (dirNames->Length>0)
      {
         for (int i=0; i<dirNames->Length; ++i) 
         {
            // Delete the Archive directory.
            isoFile->DeleteDirectory(dirNames[i]);  
         }
      }
      dirNames = isoFile->GetDirectoryNames(S"*");
      isoFile->Remove();
   }
   catch (Exception* e)
   {
      Console::WriteLine(e->ToString());
   }

}

[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 Framework Security: 

See Also

IsolatedStorageFile Class | IsolatedStorageFile Members | System.IO.IsolatedStorage Namespace

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft