Files in AppdataFolder not visible for all the use

2019-08-12 15:04发布

问题:

i have developed an addin where its setup was build using wix.When the setup being installed i need to have many files like helpdocument ,logs and Mainly Templates in that appdatatfolder .The files and folder will be automatically created when the setup got installed.

My need is

  1. Initailly my problem is, this folder and files are created only for user who installed the setup.But i want these folder and files visible for all the user including admin.
  2. That file and folder contains templates,here there are two types of templates 2.1.PRE DEFINED TEMPLATES which are included in the setup- It should be visible for all the users. 2.2.USER DEFINED TEMPLATES which are created by users when using the application and it should be only visible for the user who created it.(ie)It must be user specific.

回答1:

Anything that needs to be visible to all users should be saved into 'CommonAppDataFolder', and anything for just the current user should be saved into 'AppDataFolder'.

Just create a Directory element with the id set to the appropriate value as illustrated below:

  <Fragment>
    <Directory Id="TARGETDIR" Name="SourceDir">
      <Directory Id="CommonAppDataFolder">
        <Directory Id="INSTALLDIR" Name="CommonTemplates"/>
      </Directory>

      <Directory Id="AppDataFolder">
        <Directory Id="INSTALLDIR2" Name="UserSpecificTemplates"/>
      </Directory>
    </Directory>
  </Fragment>

Refer to this list for other common locations: https://msdn.microsoft.com/en-us/library/aa370905(VS.85).aspx#system_folder_properties



回答2:

Common files should go in the Program Data folder, which often resolves to "C:\ProgramData". In windows installer this derives from "CommonAppDataFolder". It is normally appropriate to include your company and product name in the path to minimize the possibility of clashes with other products. Using WIX:

<Directory Id="TARGETDIR" Name="SourceDir">
  <Directory Id="CommonAppDataFolder">
    <Directory Id="CompanyCommonAppDataFolder" Name="MyCompanyName">
      <Directory Id="ProductCommonAppDataFolder" Name="ProductName"/>
    </Directory>
  </Directory>
</Directory>

On a typical system this would create the folder C:\ProgramData\MyCompanyName\ProductName.

For a user specific folder, there are two sets of user profile folders: Local and Roaming. For maximum portability it is generally better to use the Local profile.

Creating a local profile folder in the installer is more of a challenge. It is possible using advertised short-cuts. When the a user runs your product from a shortcut for the first time, the advertised short-cut invokes the installer to install the component. Within this component, you could include a <CreateFolder...> element to create an additional directory in the users's profile. The user profile folder should derive from LocalAppDataFolder, and like the common folder should also include your company and product names in the path to avoid clashes with other products.

An alternative approach is for your application to create a user folder directly when it starts. Personally I prefer this way because debugging advertised short-cuts can be somewhat difficult. The function System.Environment.GetFolderPath() passing Environment.SpecialFolder.LocalAppData returns the path for the current user's local profile. Which you could use as a base for the user folder in your application.