C# C中的Microsoft.VisualBasic.FileIO.FileSystem等价性#
我使用VS2008、.NET3.5、C#项目。我需要像Microsoft.VisualBasic.FileIO.FileSystem.DeleteDirectory一样执行相同的功能 有人说从C#内部引用Microsoft.VisualBasic通常是不可取的。我觉得,在C代码中与VB的任何关联都是不可取的 使用文件系统类,这是一个完美的解决方案,但我不希望引用Microsoft.VisualBasic库。那个我会避免的C# C中的Microsoft.VisualBasic.FileIO.FileSystem等价性#,c#,vb.net,equivalence-classes,C#,Vb.net,Equivalence Classes,我使用VS2008、.NET3.5、C#项目。我需要像Microsoft.VisualBasic.FileIO.FileSystem.DeleteDirectory一样执行相同的功能 有人说从C#内部引用Microsoft.VisualBasic通常是不可取的。我觉得,在C代码中与VB的任何关联都是不可取的 使用文件系统类,这是一个完美的解决方案,但我不希望引用Microsoft.VisualBasic库。那个我会避免的 private static void DeleteDirect
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);
}
其他样本:
如果要直接删除目录,请使用,而不使用回收站,就像
DeleteDirectory
方法那样。要使用回收站,请坚持使用链接问题中提供的解决方案。您可以尝试以下方法
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo("C:\\MyDirectoryToDelete");
di.Delete(true);
甚至
System.IO.Directory.Delete("Path goes here");
希望这有帮助。相同/类似的功能在
System.IO
命名空间中可用:
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.
我不知道现有的SendToRecycleBin
等价物,但您可以尝试:
di.MoveTo("C:\\$Recycle.Bin\\S-..."); //You'd need to know the SID of the user logged in
复制示例下面的代码将为您提供与您提供的示例类似的内容:
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...
}
同样,上面的示例需要您在能够发送到回收站之前识别用户的SID。可能重复的
您可以使用Microsoft.VisualBasic和AFAIK中的FileIO,这样不会出现不合理的行为。将文件移动到回收站的方法是不可移植的。我使用的是WinXP,回收站位于C:\RECYCLER内。同意,需要标识回收站的路径,才能使其正常工作。我很犹豫是否要提到它,但我想我至少会提供一个想法,“会给你类似的东西”。如果您想要相同的功能,则必须使用
Microsoft.VisualBasic.FileIO
命名空间。从C#使用它没有什么实际问题。枚举如何:UIOption、RecycleOption、UICancelOption?从C#引用Microsoft.VisualBasic没有什么错。
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...
}