Windows 7 不允许我编辑 Common Application Data 文件夹中的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3945600/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Windows 7 doesn't allow me edit files in Common Application Data folder
提问by Ata
I want to store some files and edit them for my software in common Application Data under Windows 7.
我想在 Windows 7 下的通用应用程序数据中存储一些文件并为我的软件编辑它们。
I do not know why Windows 7 doesn't allow my software to change files unless I run them as administrator.
我不知道为什么 Windows 7 不允许我的软件更改文件,除非我以管理员身份运行它们。
Where can I store my files, so it won't require admin permission?
我在哪里可以存储我的文件,这样它就不需要管理员权限?
采纳答案by Brian
You should store your application data in a subfolder under Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
. Note that this folder is user-specific.
您应该将应用程序数据存储在Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
. 请注意,此文件夹是特定于用户的。
Non-Admin users do not have permission to write to the CommonApplicationData
folder, because that folder does not belong to specific users.
非管理员用户无权写入该CommonApplicationData
文件夹,因为该文件夹不属于特定用户。
Note that you can also store your settings by using the Settings Class. To do so, add New Item to your project, pick "Settings File". This is appropriate for basic settings like strings and numbers.
请注意,您还可以使用设置类来存储您的设置。为此,将新项目添加到您的项目中,选择“设置文件”。这适用于字符串和数字等基本设置。
回答by Michael Goldshteyn
You need to use elevation in order to modify those files, due to User Access Control. Take a look at this MSDN article for more details: MSDN article about UAC
由于用户访问控制,您需要使用提升才能修改这些文件。有关更多详细信息,请查看此 MSDN 文章:关于 UAC 的 MSDN 文章
If you are not sure how to do this in C#, here is an article: Article about requesting elevation in C#
如果您不确定如何在 C# 中执行此操作,请参阅以下文章:关于在 C# 中请求提升的文章
回答by Scott Chamberlain
Some folders are restricted starting in windows 7, these folders should not be touched unless you are installing, uninstalling, or patching. Please see my answer that you accepted from earlier todayit is the exact same case.
从 Windows 7 开始,某些文件夹受到限制,除非您正在安装、卸载或修补,否则不应触及这些文件夹。请参阅您今天早些时候接受的我的回答,这是完全相同的情况。
If you have multiple aplications that need to edit the same files they should be stored in
Environment.GetFolderPath( Environment.SpecialFolder.CommonApplicationData)
如果您有多个应用程序需要编辑相同的文件,则应将它们存储在
Environment.GetFolderPath( Environment.SpecialFolder.CommonApplicationData)
System.Environment.SpecialFolder.CommonProgramFiles
(the folder I think you are trying to write to) is read only unless you are elevated.
System.Environment.SpecialFolder.CommonProgramFiles
(我认为您正在尝试写入的文件夹)是只读的,除非您被提升。
read this articlefrom the MSDN it explains where you should be saving data and how do use the various folders correctly. My solution is the first one on his list, to summarize:
从 MSDN阅读这篇文章,它解释了您应该在哪里保存数据以及如何正确使用各种文件夹。我的解决方案是他列表中的第一个,总结一下:
You need to create a folder while you are installing the program in the CommonApplicationData folder then set permissions on that new folder so it will have write access from the users group.
您需要在 CommonApplicationData 文件夹中安装程序时创建一个文件夹,然后在该新文件夹上设置权限,以便它具有用户组的写访问权限。
回答by Xaqron
It's a security option turned on by default in windows 7 namely UAC (user Account Control). You should change your design (write somewhere else) or turn off UAC from control panel\User Accounts.
这是 Windows 7 中默认打开的安全选项,即 UAC(用户帐户控制)。您应该更改您的设计(在其他地方写)或从控制面板\用户帐户关闭 UAC。
回答by Danilo Silva
I have run into this same problem, but using the Inno Setup Installer, I have a parameter named "Permissions" where I set the value "users-full" to create a file in my application folder into the common application data with full control for all users of that file.
我遇到了同样的问题,但是使用 Inno Setup Installer,我有一个名为“Permissions”的参数,我将值“users-full”设置为在我的应用程序文件夹中创建一个文件到公共应用程序数据中,并完全控制该文件的所有用户。