写入“ProgramData”文件夹(W7 和 Vista).NET

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2362946/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 14:01:09  来源:igfitidea点击:

Write in "ProgramData" folder (W7 and Vista) .NET

.netuacwindows-vistaprogramdata

提问by aco

I install my app under "Program Files" directory. And I install data, under "ProgramData" directory:

我在“Program Files”目录下安装我的应用程序。我在“ProgramData”目录下安装数据:

Environment.SpecialFolder.CommonApplicationData

环境.SpecialFolder.CommonApplicationData

In programData I have created folder to save data. Example:

在 programData 我创建了文件夹来保存数据。例子:

C:\ProgramData\MyApp\MyData\here I have files and folders

C:\ProgramData\MyApp\MyData\here 我有文件和文件夹

Under XP all runs fine. But not under Vista or W7 OS.

XP下一切正常。但不适用于 Vista 或 W7 操作系统。

I can read files in this directory, but I can not write files, I can not create new files, etc. The user is logged as Admin.

我可以读取此目录中的文件,但无法写入文件,无法创建新文件等。用户以管理员身份登录。

Where I can store data without restrictions? I need store data in a folder visible for all users

在哪里可以不受限制地存储数据?我需要将数据存储在对所有用户可见的文件夹中

EDITED:

编辑:

I have this code in my app.manifest file:

我的 app.manifest 文件中有此代码:

<?xml version="1.0" encoding="utf-8"?>
<asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
    <security>
      <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
        <!-- Opciones del manifiesto de Control de cuentas de usuario
             Si desea cambiar el nivel de Control de cuentas de usuario de Windows, reemplace el 
             nodo requestedExecutionLevel por alguno de los siguientes.

        <requestedExecutionLevel  level="asInvoker" uiAccess="false" />
        <requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />
        <requestedExecutionLevel  level="highestAvailable" uiAccess="false" />

            Si desea utilizar la virtualización de archivos y del Registro para la compatibilidad 
            con versiones anteriores, elimine el nodo requestedExecutionLevel.
        -->
        <requestedExecutionLevel level="asInvoker" uiAccess="false" />
      </requestedPrivileges>
    </security>
  </trustInfo>
</asmv1:assembly>

采纳答案by Simon Linder

This is a UAC thing. You have your program run as administrator for creating/deleting files/folders in this SpecialFolder.

这是 UAC 的事情。您以管理员身份运行您的程序,以在此特殊文件夹中创建/删除文件/文件夹。

回答by Grokys

An easy to understand explanation of the different places you can store program data can be found here:

可以在此处找到有关可以存储程序数据的不同位置的易于理解的说明:

http://blogs.msdn.com/cHymans/archive/2008/02/05/where-should-i-write-program-data-instead-of-program-files.aspx

http://blogs.msdn.com/cHymans/archive/2008/02/05/where-should-i-write-program-data-instead-of-program-files.aspx

As regards ProgramData, it says:

至于 ProgramData,它说:

FOLDERID_ProgramData / System.Environment.SpecialFolder.CommonApplicationData The user would never want to browse here in Explorer, and settings changed here should affect every user on the machine. The default location is %systemdrive%\ProgramData, which is a hidden folder, on an installation of Windows Vista. You'll want to create your directory and set the ACLs you need at install time.

FOLDERID_ProgramData / System.Environment.SpecialFolder.CommonApplicationData 用户永远不想在资源管理器中浏览此处,此处更改的设置应该会影响机器上的每个用户。默认位置是 %systemdrive%\ProgramData,这是 Windows Vista 安装中的一个隐藏文件夹。您需要在安装时创建目录并设置所需的 ACL。

回答by Karsten

it is a conceptual issue at first look:

乍一看,这是一个概念问题:

You should NOT use the "ProgramData" directory but some user specific files. Needed installation data should installed in the "program files" or in the registry.

您不应使用“ProgramData”目录,而应使用某些特定于用户的文件。需要的安装数据应该安装在“程序文件”或注册表中。