windows 在 C:\ProgramData\ 中写入时的权限/所有者问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22107812/
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
Privileges/owner issue when writing in C:\ProgramData\
提问by Basj
As pointed out in Writing config file in C:\Program Files (x86)\MyApp\myapp.cfg, vs. Administrator privilege, it is nota good idea to write a config file in C:\Program Files (x86)\MyApp\myapp.cfg
.
正如指出的写作配置文件在C:\ Program Files文件(x86)的\ MyApp的\ myapp.cfg,对管理员权限,这是不是一个好主意写在配置文件中C:\Program Files (x86)\MyApp\myapp.cfg
。
Instead of this, my software now saves its data in a subdir of %ALLUSERSPROFILE%
(ex : C:\ProgramData\MyApp\myapp.cfg
on Win7)
取而代之的是,我的软件现在将其数据保存在%ALLUSERSPROFILE%
(例如:C:\ProgramData\MyApp\myapp.cfg
在 Win7 上)的子目录中
[I use myfile = open(filename, 'a')
in Python to do this.]
[我用myfile = open(filename, 'a')
Python 来做到这一点。]
I now encounter an issue about this file :
我现在遇到有关此文件的问题:
- I installed the software with
User A
, and ran it, then the fileC:\ProgramData\MyApp\myapp.cfg
was written. - Then, I changed user to
User B
, and ran my software again : now an error is displayed :User 2
has no right to write inC:\ProgramData\MyApp\myapp.cfg
(Permission denied
).
- 我用 安装了软件
User A
,然后运行它,然后文件C:\ProgramData\MyApp\myapp.cfg
就被写入了。 - 然后,我将用户更改为
User B
,并再次运行我的软件:现在显示错误:User 2
无权写入C:\ProgramData\MyApp\myapp.cfg
(Permission denied
)。
Why? Isn't %ALLUSERSPROFILE%
a place that can be written by all users?
How to solve this problem ?
为什么?不是%ALLUSERSPROFILE%
所有用户都可以写的地方吗?如何解决这个问题呢 ?
回答by David Heffernan
No, C:\ProgramData
, aka FOLDERID_ProgramData
, has restricted security settings. Standard users can create files there. But these files are, by default, secured so that only the user that created the file can subsequently modify the file.
不,C:\ProgramData
也就是FOLDERID_ProgramData
限制了安全设置。标准用户可以在那里创建文件。但默认情况下,这些文件是受保护的,因此只有创建该文件的用户才能随后修改该文件。
The recommended solution is for your installer to create a sub directory of C:\ProgramData
for your shared storage. And that sub directory must be given a permissive ACL by the installation program. That is what grants the desired access to all standard users.
推荐的解决方案是让您的安装程序C:\ProgramData
为您的共享存储创建一个子目录。并且安装程序必须为该子目录提供一个允许的 ACL。这就是授予所有标准用户所需访问权限的原因。
I do wonder whether you really need shared writeable data. Normally I'd expect to see shared configuration be something that is specified at install time and modified infrequently by administrators. Most configuration data tends to be per user.
我确实想知道您是否真的需要共享的可写数据。通常我希望看到共享配置是在安装时指定的并且很少由管理员修改。大多数配置数据往往是针对每个用户的。
回答by Daniel Caban
I'd like to add onto this as I was having issues writing to C:\ProgramData as well. My issue ended up being that my directory/files within C:\ProgramData were written by an administrator. When my app ran under a normal user it was unable to write there so Windows automatically used C:\Users\fooface\AppData\Local\VirtualStore\ProgramData instead. I found the path it was writing to by using process monitor on my application. After seeing this I deleted the files out of C:\ProgramData and ran my app again and it wrote there as expected.
我想补充一点,因为我也遇到了写入 C:\ProgramData 的问题。我的问题最终是我在 C:\ProgramData 中的目录/文件是由管理员编写的。当我的应用程序在普通用户下运行时,它无法在那里写入,因此 Windows 自动使用 C:\Users\fooface\AppData\Local\VirtualStore\ProgramData 代替。我通过在我的应用程序上使用进程监视器找到了它正在写入的路径。看到这一点后,我从 C:\ProgramData 中删除了文件并再次运行我的应用程序,它按预期写在那里。
Hope this helps someone.
希望这可以帮助某人。