6

I use VS 2008, .net 3.5, C# projects. I need do the same functionally like Microsoft.VisualBasic.FileIO.FileSystem.DeleteDirectory.

Anyone says referencing the Microsoft.VisualBasic is often undesirable from within C#. Any association with VB from within C# code strikes me as undesirable.

Using FileSystem class, this is a perfectly fine solution, but I prefer not references Microsoft.VisualBasic library. That one I would avoid.

     private static void DeleteDirectory(string destino)
            {
    //UIOption Enumeration. Specifies whether to visually track the operation's progress. Default is UIOption.OnlyErrorDialogs. Required.

    //RecycleOption Enumeration. Specifies whether or not the deleted file should be sent to the Recycle Bin. Default is RecycleOption.DeletePermanently.

    //UICancelOption Enumeration. Specifies whether to throw an exception if the user clicks Cancel. Required.
                Microsoft.VisualBasic.FileIO.FileSystem.DeleteDirectory(destino, 
Microsoft.VisualBasic.FileIO.UIOption.OnlyErrorDialogs, 
Microsoft.VisualBasic.FileIO.RecycleOption.DeletePermanently, 
Microsoft.VisualBasic.FileIO.UICancelOption.ThrowException);
                //Directory.Delete(destino, true);
            }

Other samples: How do you place a file in recycle bin instead of delete?

Microsoft.VisualBasic.FileIO.FileSystem.DeleteFile(file.FullName,
    Microsoft.VisualBasic.FileIO.UIOption.OnlyErrorDialogs,
    Microsoft.VisualBasic.FileIO.RecycleOption.SendToRecycleBin);
Community
  • 1
  • 1
Kiquenet
  • 14,494
  • 35
  • 148
  • 243

3 Answers3

0

The same/similar functionality is available within the System.IO namespace:

System.IO.FileInfo fi = new System.IO.FileInfo("C:\\Test.txt");
fi.Delete();

System.IO.DirectoryInfo di = new System.IO.DirectoryInfo("C:\\Test");
di.Delete(true); //Recursive, pass false for no recursion.

I'm not aware of existing SendToRecycleBin equivalent, but you could try:

di.MoveTo("C:\\$Recycle.Bin\\S-..."); //You'd need to know the SID of the user logged in

To replicate the example
The following code will give you something similar to what you have provided as your example:

try
{
    bool deletePermanently = true; //Set to false to move

    System.IO.DirectoryInfo di = new System.IO.DirectoryInfo("C:\\Test");
    if (deletePermanently)
    {
        if (di.Exists)
            di.Delete(true);
    }
    else
    {
        if (di.Exists)
            di.MoveTo("C:\\$Recycle.Bin\\S-0-0-00-00000000-000000000-0000000000-000"); //Replace with your SID
    }
}
catch
{
    Console.WriteLine("Error deleting directory"); //Add exception detail messages...
}

Again, the above example would need you to identify the SID of the user before being able to send to the recycle bin.

Jaymz
  • 6,140
  • 2
  • 26
  • 30
  • Your approach for moving a file to the recycle bin is not portable. I am on a WinXP and there the recycle bin is inside C:\RECYCLER. – Daniel Hilgarth Apr 06 '11 at 10:04
  • Agreed, the path to the recycle bin would need to be identified in order for this to work. I was hesitant to mention it, but thought I would provide the idea at least. – Jaymz Apr 06 '11 at 12:55
  • "will give you something _similar_". If you want **identical** functionality, you'll have to use the `Microsoft.VisualBasic.FileIO` namespace. There are no real issues using that from C#. – Jaymz Apr 06 '11 at 18:08
0

Possible duplicate of

System.IO Versus VisualBasic.FileIO

You can use FileIO from Microsoft.VisualBasic and AFAIK it will not behave unreasonably..

Community
  • 1
  • 1
sajoshi
  • 2,733
  • 1
  • 18
  • 22
-1

You could try the following.

System.IO.DirectoryInfo di = new System.IO.DirectoryInfo("C:\\MyDirectoryToDelete");
di.Delete(true);

Or even

System.IO.Directory.Delete("Path goes here");

Hope this helps.

Jethro
  • 5,896
  • 3
  • 23
  • 24