如何在Windows7的C:\ Program Files(x86)\ myAplication中创建文件?
嗨.
我有一个VS 2008解决方案,其中有2个项目.
一个项目是输出myApp.exe
的实际应用程序,另一个项目是链接到第一个项目的VS安装项目.
安装程序会复制myApp.exe
和C:\Program Files (x86)\appDir
中的依赖项. myApp.exe
在同一文件夹中尝试创建新文件,但没有成功.myApp
中的代码看起来像这样:
Hi.
I have a VS 2008 solution in which there are 2 projects.
One project is the actual application that outputs myApp.exe
and the other project is a VS setup project linked to the first project.
The installer copies myApp.exe
and the dependencies in C:\Program Files (x86)\appDir
. In this same folder myApp.exe
is trying, with no success, to create a new file.
The code in myApp
looks something like this:
File.WriteAllLines(
Application.StartupPath + "myFile.txt",
new string[]{"test1", "test2", "test3"});
在Windows XP上可以正常使用.
在Windows 7上,我得到一个System.UnauthorizedAccessException
,内容为:"Attempt to perform an unauthorized operation"
.我认为这是因为appDir
文件夹是在安装时使用管理员权限创建的,而在运行时myApp.exe
会尝试使用用户权限进行写入.
问题1:如何修改安装项目,以便它可以通过用户写入权限创建appDir
?
或
问题2:如何编写代码,以便系统允许我创建新文件而不受访问权限的约束?
有人可以帮我解决这个问题吗?
谢谢!
PS:我已经尝试过了,但是也失败了:
On Windows XP this works just fine.
On Windows 7 I get a System.UnauthorizedAccessException
that reads: "Attempt to perform an unauthorized operation"
. I believe this is because on install the appDir
folder is created using Administrator permissions and on run-time myApp.exe
tries to write using User permissions.
Q1: How can I modify the setup project so that it can create the appDir
with User writing access?
OR
Q2: How can I write the code so that the system allows me to create a new file regardless of access permissions?
Can anyone please help me with this issue?
Thank you!
PS: I''ve tried this and it fails as well:
DirectoryInfo dInfo = new DirectoryInfo(Application.StartupPath);
DirectorySecurity dSecurity = dInfo.GetAccessControl();
FileSystemAccessRule fsar = new FileSystemAccessRule(
WindowsIdentity.GetCurrent().Name,
FileSystemRights.WriteData,
AccessControlType.Allow);
dSecurity.AddAccessRule(fsar);
dInfo.SetAccessControl(dSecurity);
您不应该这样做.我知道很多应用程序都是在XP下执行此操作的,但是这种行为迫使您在Vista和Weven下以管理员身份运行应用程序.
如果要在应用程序的所有用户之间共享数据,则安装程序应向用户查询应在其中存储数据的路径,创建路径并对其进行引用(在HKLM中或通过App.机制).
(这是使您的"Windows 7认证"考试不及格的最快方法:))
Google在Vista中有关文件夹虚拟化的文章,您将了解我在说什么.
Vista和Seven的一般原则是HKLM,\ Program程序文件仅对于正常操作是只读的(它们在XP中,但是每个人都以管理员身份运行).
You should not be doing this. I know a lot of applications did this under XP, but this kind of behaviour forces you to run your app as Administrator under Vista and Weven.
If you want to share data between all users of the application, your installer should query the user for a path where that data should be stored, create the path, and put a reference to it (either in HKLM, or through the App.Settings mechanism) somewhere.
(This is the fastest way of flunking your "certified for Windows 7" exam :-) )
Google articles on folder virtualization in vista, and you''ll see what I''m talking about.
The general principle under Vista and Seven is HKLM and \Program Files are read only for normal operations (they were in XP, but everybody was running as administrator).
的建议,您应该使用ApplicationData或CommonApplicationData文件夹.
如果您决定走这条路线,请此提示 [ ^ ]可能会有所帮助.
Along the lines of Michel''s suggestion, you should be using the ApplicationData or CommonApplicationData folders.
If you decide to go this route, this tip[^] may help.
Windows Vista/Seven中有许多系统文件夹.这文章 [
There are a number of system folders available in Windows Vista/Seven. This article[^] describes what each of the system folders are and where in Windows Vista/Seven the folders map to by default.
Global shared data usually gets mapped to C:\ProgramData, for example. User specific data is usually mapped to C:\Users\[username]\AppData\Local or C:\Users\[username]\AppData\Roaming. If you''re maintaining compatibility with Windows XP, it''s best to use the %AppData% environment variable rather than the %LocalAppData%, as the %LocalAppData% EV doesn''t exist in Windows XP.
To access these locations in .NET, check out Environment.SpecialFolder in the MSDN documentation.
Hope this helps.
Flynn