Reputation: 2142
I have two versions of an Installer to my C# application. Say V1 and V2.
I have installed V1. And in the registry setting of the setup project I have created a registry key InstallDir= [TARGETDIR]
which gives the installation folder of my application.
So when I want to get the installation folder I could get the Path by using the registry key I have generated.
The problem is during the installation of version 2 V2, The file say example.txt in my previous version installation folder should be copied to somewhere.
I have created custom actions in my installer class in Install state like follows.
public override void Install(IDictionary stateSaver)
{
base.Install(stateSaver);
string path = null;
string registry_key = @"SOFTWARE\";
using (Microsoft.Win32.RegistryKey key = Registry.LocalMachine.OpenSubKey(registry_key))
{
foreach (string subkey_name in key.GetSubKeyNames())
{
if (subkey_name == "default Company Name")
{
using (RegistryKey subkey = key.OpenSubKey(subkey_name))
{
path = (string)subkey.GetValue("InstallDir");
}
}
}
}
string fileName = "example.txt";
string sourcePath = path;
string targetPath = @"C:\Users\UserName\Desktop";
// Use Path class to manipulate file and directory paths.
string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
string destFile = System.IO.Path.Combine(targetPath, fileName);
// To copy a folder's contents to a new location:
// Create a new target folder, if necessary.
if (!System.IO.Directory.Exists(targetPath))
{
System.IO.Directory.CreateDirectory(targetPath);
}
// To copy a file to another location and
// overwrite the destination file if it already exists.
System.IO.File.Copy(sourceFile, destFile, true);
}
What I thought is, if I specify the path from registry in the Install method of custom Action it will take the Previous version path and copy the file in the previous version installation path.
But even If I copy in the Install method of Custom Action the registry has been updated with the path of newer version and takes the current value and updates with the newer version file.
But I need previous version file in that installation folder.
How could I achieve that?
Upvotes: 1
Views: 1870
Reputation: 2142
After some research I figured out a way to extract a single file from the previous Installation when we install a newer version.
Add a Seperate Console Application to copy the file as @AccessDenied said.
Add a registry key [ProductCode] in the HKEY_LOCAL_MACHINE\Software in the registry option of installer with a subkey installdir
with value [TARGETDIR].
Have the product Code of older version and store it somewhere, say a configuration file.
In the Installer Class two cases should be handled. Check the registry to check whether the application already installed.
Case 1:
If not Installed already, run the Console Application to copy it to the current installation folder.
or Case 2:
search for the ProductCode
of the previous version in the registry under key installed structure.
(In case if you want to update the file frequently say if it is a SQLite file, then have a separate class to update the file in the project)
On finding it, Extract the subkey value installdir
and value which can be assigned to a path.
Then Use that path to copy the required file and store it.
Adv:
Previous version can be uninstalled.
Single Installation.
Copying the required file from the previous installation
Edit: A simple solution
In custom action Install method copy the file required for future use in the current installation folder and put it in some folder of the same installation folder.
public override void Install(IDictionary stateSaver)
{
base.Install(stateSaver);
string path = null;
string registry_key = @"SOFTWARE\";
// In my case I am getting the user selected current installation folder from the registry
using (Microsoft.Win32.RegistryKey key = Registry.LocalMachine.OpenSubKey(registry_key))
{
foreach (string subkey_name in key.GetSubKeyNames())
{
if (subkey_name == "Default Company Name")
{
using (RegistryKey subkey = key.OpenSubKey(subkey_name))
{
path = (string)subkey.GetValue("InstallDir");
}
}
}
}
string fileName = "example.txt";
string sourcePath = path;
//Have created a folder named "tmp" in current innstallation folder.
string targetPath =System.IO.Path.Combine(path,"tmp");
// Use Path class to manipulate file and directory paths.
string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
string destFile = System.IO.Path.Combine(targetPath, fileName);
// To copy a folder's contents to a new location:
// Create a new target folder, if necessary.
if (!System.IO.Directory.Exists(targetPath))
{
System.IO.Directory.CreateDirectory(targetPath);
}
//Copy the required file.
System.IO.File.Copy(sourceFile, destFile, true);
}
Now even If we uninstall the tmp folder wont be deleted. While installing the another version we can access the previous version file from the tmp folder.
Thanks.
Upvotes: 0
Reputation: 8902
I would recommend you to follow this approach,
So your problem is your Sqllite file should be in the installation folder and when you install the new MSI version it un-installs the existing application which in trun removes all the files including Sqllite,
You can solve this by creating a console application
. Add the sqllit file to console application and when console exe is executed it should copy the sqllite files into currently executing folder
AppDomain.CurrentDomain.BaseDirectory;
Add this console exe to your MSI project and within installer class execute this exe using Process.Start()
. Since this sqllite file is copied by different exe application and since this sqllite file is not part of the MSI project when MSI is being uninstalled it will leave the sqllite file in the folder without deleting.
Hope this will resolve your problem.
Upvotes: 1