14

I have a Setup/Deployment Project for my Application and it outputs certain files (*.dll, *.dat) to the Application Folder.

I would like a condition to be set to check if a file currently exists or not. If it doesn't, write it but if it does, don't install it from the package. Is it possible?

The file is called "database.dat" and under the Properties, I see a "Condition" attribute, but I'm not familiar with what to put in there.

Any input is greatly appreciated. Thanks in advance.

Edit:

Reason why it would already exist is that from a previous install there would be data from a DataSet / Data Table which we don't want to overwrite.

mastofact
  • 540
  • 1
  • 6
  • 23

5 Answers5

35

To only install a file, if it doesn't already exist, follow these steps:

  1. Add a "Search Target Machine" entry under the "Launch Conditions" view in your setup project.

  2. fill in the FileName property and the Folder property.

  3. the Property property should be a constant you can remember, like "MY_AWESOME_FILE_EXISTS"
  4. in the "File System" view of your project, locate the component to install and add this to the Condition property "not MY_AWESOME_FILE_EXISTS"

That is all.

Sources (since I just had to figure this out for myself):

Daren Thomas
  • 67,947
  • 40
  • 154
  • 200
  • 1
    You are a beautiful person. Thank you for this, my closest friend. – ACK_stoverflow May 08 '12 at 18:32
  • @ACK_stoverflow, thank you for just making my day :) This is the first piece of internet I read today. I'm smiling big time right now! – Daren Thomas May 09 '12 at 07:01
  • 3
    `Folder` can contain any of the system folder names decribed here: http://msdn.microsoft.com/en-US/library/aa372057(v=vs.80).aspx, e.g. `[AppDataFolder]ProductName\SubFolder`. – bernhof Jul 26 '13 at 09:42
  • This fails when I tried for upgrade but works for normal installation. Any idea why it doesn't work at upgrade? – RJN Mar 05 '18 at 15:24
  • 1
    I am afraid that it fails for upgrades, because upgrades start with an uninstall, so the file is not there, the condition is true and the copy happens – Timores Mar 19 '21 at 15:53
2

You should just install the data file as a test to see what actually happens. The reason I say this is that Windows Installer will not overwrite files that have been changed after they were first installed. See this rule:

https://msdn.microsoft.com/en-us/library/windows/desktop/aa370531(v=vs.85).aspx

It seems to me you may need to do nothing at all.

PhilDW
  • 20,260
  • 1
  • 18
  • 28
0

The Condition-attribute has just what you need: an Exists-condition. Simplified example is:

<Copy Condition="!Exists($(DestPath)database.dat)"
      SourceFiles="$(SrcPath)database.dat"
      DestinationFolder="$(DestPath)"/>

See also this topic.

Community
  • 1
  • 1
0

Unfortunately, Darren's solution does not work if you use the [TARGETDIR] folder. In my case, I have a winfomrs app that uses an Sqlite db, and I don't won't to overwrite the DB when upgrading the app. So, I used the Registry check instead, when the app is installed a key is created in the registry, and removed when unistalled. So when upgrading, I check if the registry key exists, and if it does, I prevent the blank db to be copied using the database file Condition property as already explained by Darren. And it worked correctly.

-2

On the Setup Project, right-click on the file you want to keep on the installation folder, select Properties and set Permanent to true.

Antonio Leite
  • 460
  • 3
  • 7
  • This doesn't solve the problem. If you mark as permanent and uninstall, then change the file and install again it will be overwritten. – Guge Dec 17 '15 at 13:59
  • The Permanent option just ensures that the file is not removed when uninstalling the application. – AdvApp Mar 14 '21 at 01:08