Microsoft.VisualBasic.FileIO.FileSystem equivalence in C#
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.Recycle开发者_运维百科Option.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);
Possible duplicate of
System.IO Versus VisualBasic.FileIO
You can use FileIO from Microsoft.VisualBasic and AFAIK it will not behave unreasonably..
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.
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.
精彩评论