Microsoft.VisualBasic.FileIO.FileSystem equivalenc

2019-06-22 15:43发布

问题:

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);

回答1:

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.



回答2:

Use Directory.Delete, if you want to delete the directory directly, without using the recycle bin, as your DeleteDirectory method does. To use the recycle bin, stick with the solution provided in the question you linked.



回答3:

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.



回答4:

Possible duplicate of

System.IO Versus VisualBasic.FileIO

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