C# Deployement retaining files over an installatio

2019-03-03 11:12发布

问题:

I have created a Setup and deployment project using the Visual studio and install the setup. After i install the setup it copies a few files(XML) which on using of the application are configured programmatically . Now , If the user is reinstalling this setup again i need to ask the user whether these configured files need to be overwritten or be retained ??

Any idea as to how this can be accomplished ?

Thanks & Regards, Fran

回答1:

Look into file versioning rules for Windows Installer.

In short, assuming that these XML files you refer to are unversioned text files, MSI will compare the Created and Modified dates and will not replace the updated XML files which you say are updated programmatically (post-install-time).

I would suggest several other variables you need to consider to make sure things are working as you expect: major vs. minor upgrade, and the REINSTALLMODE property.



回答2:

I find that the best way to approach this kind of scenario is to implement the "preserve changes" logic in your application as opposed to via the setup. This avoids complicating your setup and yields greater control of the config process since all logic is embedded in your main EXE file. This means you can step through the process and debug it the normal "development way".

To achieve this you can install your "base config" files to a read-only location such as

  • %ProgramFiles%\MyCompany\MyApp\MyConfig*.*

Then your application can detect on launch whether existing config files exist in the user profile (or in a writable shared location), and ask the user whether the new config files should overwrite the existing config or not. You can also easily implement backup functionality for the old config.

In order to ask the question only once per user after deployment, the normal apporach is to flag HKLM with the latest installed version of the application and then write a corresponding flag in HKCU when the copy operation has completed or the user dismissed it:

  • HKLM\Software\MyCompany\MyApp\Version = 2.0.0
  • HKCU\Software\MyCompany\MyApp\Version = 1.0.0

In the above scenario version 2.0.0 of the application has been installed, but the per user config copy has not run for the user in question yet. Once it has run the HKCU version will be set to 2.0.0 and the operation is not run again until HKLM is incremented.