我用VS 2008,.NET 3.5,C#项目。我需要做同样的功能就像Microsoft.VisualBasic.FileIO.FileSystem.DeleteDirectory。
说,任何人都引用Microsoft.VisualBasic程序往往是从C#中的无益。从C#代码用VB任何关系令我不可取的。
使用FileSystem类,这是一个完全正常的解决办法,但我不想引用Microsoft.VisualBasic程序库。那一个我会避免。
私有静态无效DeleteDirectory(字符串DESTINO) { // UIOption枚举。指定是否视觉跟踪操作的进度。默认值是UIOption.OnlyErrorDialogs。需要。 // RecycleOption枚举。指定删除的文件是否应该被发送到回收站。默认值是RecycleOption.DeletePermanently。 // UICancelOption枚举。指定是否抛出一个异常,如果用户单击取消。需要。 Microsoft.VisualBasic.FileIO.FileSystem.DeleteDirectory(DESTINO, Microsoft.VisualBasic.FileIO.UIOption.OnlyErrorDialogs, Microsoft.VisualBasic.FileIO.RecycleOption.DeletePermanently,微软。 VisualBasic.FileIO.UICancelOption.ThrowException); //Directory.Delete(destino,真正的); }
其他样本:的How你放到回收站中的文件,而不是删除吗?
Microsoft.VisualBasic.FileIO.FileSystem.DeleteFile(file.FullName, Microsoft.VisualBasic.FileIO.UIOption。 OnlyErrorDialogs, Microsoft.VisualBasic.FileIO.RecycleOption.SendToRecycleBin);解决方案
同样/类似的功能是在现有的 System.IO 命名空间:
System.IO.FileInfo F1 =新的系统。 IO.FileInfo(C:\\Test.txt); fi.Delete(); System.IO.DirectoryInfo DI =新System.IO.DirectoryInfo(C:\\Test); di.Delete(真); //递归,对于没有递归传递false。
我不知道现有的 SendToRecycleBin 等价的,但你可以尝试:
di.MoveTo(C:\\ $ Recycle.Bin\\小号-...); //你需要知道登录用户的SID
复制例如结果下面的代码会给你类似的东西你有什么作为你的例子提供的:
试 {布尔deletePermanently = TRUE; //设置为false移动 System.IO.DirectoryInfo DI =新System.IO.DirectoryInfo(C:\\Test); 如果(deletePermanently) {如果(di.Exists) di.Delete(真); } ,否则 {如果(di.Exists) di.MoveTo(C:\\ $ Recycle.Bin\\S- 0-0-00-00000000-000000000-0000000000-000); ; //你的SID } } 抓 { Console.WriteLine(错误删除目录)替换//添加例外详细信息... }再次上面的例子就需要你能够发送到回收站之前识别用户的SID。
I use VS 2008, 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);解决方案
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 inTo 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.
更多推荐
Microsoft.VisualBasic.FileIO.FileSystem等价于C#
发布评论