C# “程序文件”文件夹下的文件写入权限问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1191941/
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
file write permission issue under "Program Files" folder
提问by George2
I am using inno setup to make a installation package for my application, and my application is written by C# + .Net 2.0 + VSTS 2008. Inno setup => http://www.jrsoftware.org/isinfo.phpand I install my application under Program Files/Foo folder (Foo is my application name). My application is targeting to Windows Vista.
我正在使用 inno setup 为我的应用程序制作安装包,我的应用程序是由 C# + .Net 2.0 + VSTS 2008 编写的。Inno setup => http://www.jrsoftware.org/isinfo.php并且我安装了我的Program Files/Foo 文件夹下的应用程序(Foo 是我的应用程序名称)。我的应用程序面向 Windows Vista。
The issue I found is my program cannot write to the folder Program Files/Foo. And I need the permission of write to this folder in order to save some configuration files. The strange thing I notice is the folder Program Files/Foo is marked as readonly and I have checked all folders under Program Files are marked with read only, such as Office.
我发现的问题是我的程序无法写入文件夹 Program Files/Foo。我需要写入这个文件夹的权限才能保存一些配置文件。我注意到的奇怪的事情是文件夹 Program Files/Foo 被标记为只读,我检查了 Program Files 下的所有文件夹都标记为只读,例如 Office。
My questions are,
我的问题是,
- Why all folders are marked as read only under Program Files? It means we should not write to individual application folders under Program Files? If not, where should we write information to disk like user last selected configuration information of an individual application?
- If we could write to individual application folders under Program Files, what is the solution? I do not want my application to Run As administrator to solve this issue, and if there are solution to write to this folder, I want to require minimal permission if possible.
- 为什么所有文件夹在 Program Files 下都标记为只读?这意味着我们不应该写入 Program Files 下的单个应用程序文件夹?如果没有,我们应该在哪里将信息写入磁盘,例如用户上次选择的单个应用程序的配置信息?
- 如果我们可以写入 Program Files 下的单个应用程序文件夹,解决方案是什么?我不希望我的应用程序以管理员身份运行来解决这个问题,如果有写入这个文件夹的解决方案,如果可能的话,我希望需要最少的权限。
采纳答案by Paul van Brenk
You should write user specific config data to the Application Data folder for the current user, using the special folders enumand the Enivronment.GetFolderPath.
您应该使用特殊文件夹 enum和Enivronment.GetFolderPath将用户特定的配置数据写入当前用户的 Application Data 文件夹。
回答by jpoh
A common solution would be to install configuration files to the Application Data folder i.e. like follows:
一个常见的解决方案是将配置文件安装到 Application Data 文件夹中,如下所示:
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
回答by AndrewS
Best Practice is to not store config data in the Program Files folder. Instead, store your application's data in %AppData%\YourApplicationName. Depending on whether you want to store your config data per-user or in a shared common folder, use one of the following enums to get the folder path:
最佳实践是不要将配置数据存储在 Program Files 文件夹中。相反,将应用程序的数据存储在 %AppData%\YourApplicationName 中。根据您是要按用户存储配置数据还是在共享的公共文件夹中存储配置数据,请使用以下枚举之一来获取文件夹路径:
string userAppData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string commonAppData = Envrionment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
By default, Vista users do notrun programs as Administrators and hence those programs have only read accessto the folders under "Program Files". Users can change this behavior by disabling UAC and you couldask your users to do that, but in an office setting users might not have that option. That's why you use AppData instead -- applications can always read andwrite data to the AppData folder.
默认情况下,Vista 用户不会以管理员身份运行程序,因此这些程序只能读取“程序文件”下的文件夹。用户可以通过禁用 UAC 来更改此行为,您可以要求您的用户这样做,但在办公室设置中,用户可能没有该选项。这就是您改用 AppData 的原因——应用程序始终可以在 AppData 文件夹中读取和写入数据。
Information on UAC can be found at Microsoft's site. Although this page is fairly long, it's a starting point for understanding UAC: http://msdn.microsoft.com/en-us/library/bb530410.aspx
有关 UAC 的信息可以在 Microsoft 的站点上找到。虽然这个页面相当长,但它是理解 UAC 的起点:http: //msdn.microsoft.com/en-us/library/bb530410.aspx