在 C# 中将目录 NAME 设为只读

发布于 2024-11-10 07:20:51 字数 139 浏览 2 评论 0原文

我的问题是,我想创建一个新文件夹,并使用户无法(或相当困难)更改其名称或删除它。问题是,用户必须能够访问该文件夹中包含的文件并以他喜欢的任何方式更改它们。使用我在网上找到的示例,我得到的只是无法更改文件夹内的文件,而不是文件夹本身。

提前致谢 ;)

my problem is, I want to create a new folder and make it impossible (or reasonably hard) for the user to change its name or to delete it. The thing is, the user must be able to access the files contained within that folder and change them in any way he pleases. Using the examples I've been finding in the net all I get is making it impossible to change the files INSIDE the folder, and not the folder itself.

Thanks in advance ;)

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

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

发布评论

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

评论(3

梦年海沫深 2024-11-17 07:20:51

只要该文件夹是由用户帐户创建的(假设您通过应用程序以编程方式创建该文件夹),用户就能够编辑该文件夹。保护该文件夹免遭篡改的最佳方法是编写一个非常小的 Windows 服务,使该文件夹始终保持打开状态,从而防止删除/重命名。

As long as that folder is created by the user's account (assuming that you're creating the folder programmatically by your application), the user will be able to edit the folder. The best way to protect that folder from tampering would be to write a very small windows service that keeps that folder always open, thus preventing deletion/renaming.

难得心□动 2024-11-17 07:20:51

这可能会有所帮助。

http://technet.microsoft.com/en-us/library/cc732880.aspx

看来您想允许“创建文件/写入数据”权限,但不允许“控制”父文件夹。

您应该能够设置 ACL 来执行此操作。授予他们“列出文件夹内容”权限,然后有选择地授予他们其他扩展权限,而不授予他们修改属性权限。


服务答案是一个坏主意。我可能会工作,但这不是最好的方法。 Windows目录和文件夹安全的关键是文件夹的“所有者”。作为管理员,您始终可以拥有文件夹或文件的所有权。但是,如果该文件有不同的所有者,并且该所有者已授予您权利,则在您进入并取得所有权之前,您将不拥有任何其他权利。

您想要做的是在计算机上创建一个特殊帐户(通常称为服务帐户),这是程序运行时使用的身份。该帐户具有管理员权限,并且是其创建的任何文件的所有者。然后它可以允许它想要授予其创建的文件和文件夹的用户任何访问权限。

如果管理员愿意,他们总是能够获得所有权,但大多数用户甚至不知道如何执行此操作。

This might be helpful.

http://technet.microsoft.com/en-us/library/cc732880.aspx

It seems you want to allow the "Create Files/Write Data" permission but not allow "control" of the parent folder.

You should be able to set up an ACL to do this. Give them "List folder contents" rights and then selectively give them additional extended rights without giving them modify attributes rights.


The service answer is a bad idea. I might work, but is not the best way to do it. The key with windows directory and folder security is the "owner" of a folder. As an administrator you can always take ownership of a folder or file. BUT if the file has a different owner and that owner has granted you rights you won't have any other rights until you go in and take ownership.

What you want to do is create a special account on the machine (often called a service account) which is the identity the program runs under. This account has admin rights and is the owner of any files it creates. Then it can allow whatever access it wants to grant to users of files and folders it creates.

The admin will always be able to take ownership if they want to, but most users don't even know how to do this.

夜未央樱花落 2024-11-17 07:20:51

如果您打算一直打开 C# 程序来执行任务,请根据 @Teoman Soygul 的回答您可以将此行添加到代码中,这将在要“保护”的目标目录上打开一个流,因此不允许用户修改它:

File.Open(@"yourDirHere.zip", FileMode.Open, FileAccess.Read);

此方法在目录被压缩或在文件,不在标准目录中。
这是我的用例,我认为这里值得一提,以防它对其他人也有用。您可能有兴趣使用 文件.打开方法

If you are going to have a C# program open all the time to perform your tasks, based on @Teoman Soygul's answer you could add this line to your code, which will open a stream on the target directory to be 'protected' and thus, not allow users to modify it:

File.Open(@"yourDirHere.zip", FileMode.Open, FileAccess.Read);

This method only works if the directory is compressed or on files, not on standard directories.
This was my use case and thought it was worth mentioning here, in case it could also be useful to anyone else. You may be interested in playing around te different accessors provided at File.Open Method

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