用C#删除文件夹

发布于 2024-10-03 00:54:01 字数 85 浏览 0 评论 0原文

我是 C# 新手,我将制作一个简单的工具,其中有一个按钮可以删除文档和设置中的所有文件夹,但不删除管理员文件夹。

有人可以告诉我该怎么做吗?

I'm new on C# and I will make a simple tool that has a button to delete all folders in documents and settings, but not the administrator folders.

Can some one tell me how I can do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

长亭外,古道边 2024-10-10 00:54:01

您可以使用目录信息

System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo("your path");
if (dir.Exists)
     dir.Delete(true);

You can use DirectoryInfo

System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo("your path");
if (dir.Exists)
     dir.Delete(true);
聚集的泪 2024-10-10 00:54:01

这里有很多争论,到目前为止提供的答案在技术上是可行的。但让我们尝试不同的方法...为什么你想这样做?正如您可能已经从迄今为止的回应中猜测到的那样,这可能不是一个好主意。那么,也许有了这个软件所解决的需求的一些背景,我们也许可以提供更有用的答案?

编辑:
您打算使用 USB 记忆棒走到每台 PC 上进行批量删除吗?看起来仍然不是一个好的方法。一些快速的谷歌搜索刚刚出现了这个,这可能对你有用。最好的部分是,它可以远程工作。这样就可以消除任务中“走到每台电脑前”的部分。

There's a lot of argument going on here, and the answers provided so far will technically work. But let's try a different approach... Why do you want to do this? As you may have surmised by the responses so far, this is probably not a good idea. So maybe with some background on the need being addressed by this software we can perhaps provide more useful answers?

Edit:
So you're planning to walk around to each PC with a USB stick and mass-delete? Still doesn't seem like a good approach. Some quick Googling just turned up this, which may work for you. Best part, it works remotely. So that'll remove the "walking around to each PC" part of your task.

纸短情长 2024-10-10 00:54:01

您可以使用System.IO.DirectoryInfo,然后调用Delete(true)方法来递归删除指定文件夹中的所有文件夹和文件。

MSDN 目录信息

现在仅删除非管理员文件夹是指管理员拥有的文件夹还是管理员拥有的文件夹。此外,您将无法删除当前用户不拥有的文件夹,因此您无论如何都会得到一些例外,只是盲目删除。

编辑以响应一些不同的评论

您实际上可以对 DirectorySecurityFileSecurity (我认为这是文件)进行一些迭代来找出目录或文件的所有者组,并从那里确定它是否是管理员。

You can use System.IO.DirectoryInfo and then call the Delete(true) method to recursively delete all of the Folders and files within your specified folder.

MSDN Directory Info

Now to only delete the non-Administrator folders do you mean the ones owned by administrator or the ones owned by an administrator. Also you won't be able to Delete folders that the current user doesn't own, so you are going to get some exceptions off of this anyways just blindly deleting.

Edit in response to some various comments

You can actually do some iterating over the DirectorySecurity and FileSecurity (I think that's the file one) to figure out the owners group for the Directory or File, and from there determine if it's an admin.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文