How to check if c# project installation path exists - c#

I want to get the installed path of a C# project setup. In C#.net. How can I use this path in Installer Class for checking that the setup is installed or not?
Actually I want to check if the setup is installed in the system or not . If it is, I want to repair this.

Create a register entry with the installer.
hkey_local_machine/software/ and a var installdir with value the path.
When you have an update version of your program, check in the installer class for that key. If it exists update, else new install.

You can get the path of the folder that you are installing to from
Context.Parameters["targetdir"]
So you can check that is empty before installing. Obviously that won't tell you if there are other versions installed elsewhere.

I don't understand exactly what you want but you could simply check whether a directory exists by using Directory.Exists method
Environment.SystemDirectory gets the system directory on the machine.

Related

C# How to get current directory path

Scenario :
I've created a library project which provides interface to installer(exe created using install shield).I want to check whether a particular file exists in the installer folder.
I've tried following :
1). Assembly.GetEntryAssembly().Location
// Throws "Object reference not set to an instance of an object"
2). new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath
// return :
C:\Windows\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll
3). Assembly.GetExecutingAssembly().Location
// returns empty string
4). AppDomain.CurrentDomain.BaseDirectory
// returns "C:\Windows\syswow64\" irrespective of the actual path
Could anyone tell me how can I get directory path from where the installer is being executed?
You can use
Directory.GetCurrentDirectory();
to get current directory path
import System.IO
I will answer my own question, It's not possible to get current path from library project. You'll have to get it from installer. There is no other way.
Thank you.
If you want to get the folder where your exe is resting
AppDomain.CurrentDomain.BaseDirectory
this returns the whole path to the Folder which contains the exe. From there on you can add your folders manually
AppDomain.CurrentDomain.BaseDirectory + #"MyFolder1\MyFolder2";
Assuming you are running an MSI-based install built by InstallShield:
The problem you're having is because of the way those calls work. You're implementing this in an external Dll, and Dlls don't have their own current directory - they have the current directory of the process that's calling into the Dll. In this case you're being called from an msiexec.exe process (assuming you are doing this in a custom action) so you'll get msiexec.exe's current directory.
Apart from that, some other observations:
You don't say exactly when you are calling your code, but it might be before the user has chosen the installation folder, so the search doesn't really help.
InstallShield probably provides support for a file search. If this is an MSI setup it definitely does.
Installs hardly ever look for files in an install folder because there is rarely a good reason. If you need the file, add it to your install, or if it's a redistributable then add the standard redist package, maybe a merge module. If that file belongs to another setup, what will you do if that product is uninstalled? Are you checking the version to see if it's compatible with your application? In general, all setups install the files they need and shared files are backwards compatible if they are used by several different products.
Try this one, it always works for me:
Environment.CurrentDirectory
use above property as follow.
string currentDirectory = Path.GetDirectoryName(Environment.CurrentDirectory);
If you want that path in the InstallShield use this
Setup.exe - Use the SETUPEXEDIR property
http://www.installshield.com/news/newsletter/0206-articles/SetupExeDir.asp.
Depending on version of installshield you are using try different properties
1. SRCDIR
2. SETUPEXEDIR
3. SUPPORTDIR
If you have a C# code to read preperties use MsiGetProperty like below
MsiGetProperty( hMSI, "SETUPEXEDIR", Value, Size )
string assemblyPath = Context.Parameters["assemblyPath"];
int i = assemblyPath.Length - 1;
while (assemblyPath[i] != '\\') --i;
string folder = assemblyPath.Substring(0, i);

Set installation path in windows Application Setup

i created a setup application for a windows forms application, i changed th "Default Location" property to [D][GestionStock][GestionStock] in order to install the application in this directory and i set "AlwayCreate" property to true.
but this doesn't create the custom folders and install the application directly in "D" drive.
How to set installation path to a custom folder like [D][CustomFolder][CustomFolder] and create folders during the installation?
It looks as if you are inventing property names that don't exist, such as D and GestionStock in square brackets. The brackets mean that they are Windows Installer properties, either standard or created by something in your setup. So D is not a property name, and neither is GestionStock.
These are the standard properties:
https://msdn.microsoft.com/en-us/library/windows/desktop/aa370905(v=vs.85).aspx
So if you want a folder with GestionStock in the name, just use it without the brackets. This might also be useful because it describes how to construct folder locations using strings and properties:
https://www.red-gate.com/simple-talk/dotnet/visual-studio/getting-started-with-setup-projects/
such as [ProgramFilesFolder][Manufacturer]\NewSetup where the bracketed names are property names.
The main problems you have are:
Setups are expected to install to standard folder location property names such as [ProgramFilesFolder], [CommonFilesFolder] and so on because they work on all systems. In your case your setup is assuming that all systems have a D: drive, which they don't. If you want to try installing to the D: drive, just try D:\Gestion\Gestion if that is the location, forgetting brackets.
There is a browse dialog that users expect so that they can choose the installation location, not you.
Windows really wants you to install your binaries to ProgramFiles, for the app and the install to store data in AppDataFolder (the Windows Installer property name) and so on. Going against this model will make your setup more difficult.
1.Type REGISTRY EDITOR in the search bar and click on it. It will as for permission click on YES.
2.Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion
3.Follow the above path.
4.Change ProgramFilesDir path to D:\Program Files
5.Change ProgramFilesDir(x86) path to D:\Program Files(x86)
And you're good to go. The default installation directory is changed from C:\ to D:\ in your WINDOWS.

Find / Predict *.msi file installation location reading *.msi database

Whenever we install a software, Windows OS will keep a backup copy of installer *.msi at C:\Windows\Installer\ location.
So I am iterating through all the msi file present at "C:\Windows\Installer\"
I want to prepare a mapping list where list will have *.msi file and its default location.
Let say C:\Windows\Installer[some product key]\123xR23.msi
Consider "123xR23.msi" is backup file for WinZip installation. And installer has install WinZip at location "C:\Program Files(x86)\WinZip"
So list will have following entry
"123xR23.msi","C:\Program Files(x86)\WinZip"
Please help me.
Thanks
First off, why do you even need this?
There is a mapping of these *.msi files already in existence in the registry at HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\\Products\\InstallerProperties then the value of the msi is under LocalPackage
The S-1-5-18 is the Security ID of Local System.
The ProductGUID is also a bit weird here. In your installer you will see something like
2F7DBBE6-8EBC-495C-9041-46A772F4E311
but the product GUID in the registry path will look like this
6EBBD7F2CBE8C5940914647A274F3E11
There's definitely a reason for this but I don't know what it is. You'll just have to transform your product guid to this other representation when looking in the registry.
There is also a registry key called InstallLocation which should contain the install location of the msi package. This isn't always filled in though. Perhaps the install directory is stored somewhere else in the registry I don't know you'll have to look around probably.
Edit: Looks like each component of the install gets an entry by GUID in the registry as well which contains the path it was installed to. This could help you find the install location of a specific MSI if it is not listed under InstallLocation mentioned above. Per Micheal's comment, I would suggest taking advantage of the Msi related functions (https://msdn.microsoft.com/en-us/library/windows/desktop/aa372420(v=vs.85).aspx)

Visual Studio Installer with to set path of script file

I have a Visual Studio installer that installs a Windows Service hosting a Web Service.
What I am trying to resolve is to give users during installation a choice of where a particular script is located on the box and once they have set the location to then update the location to the app.config file which the application can then use.
At the moment during install I have added a custom folder and added to that the folder the file. This all gets installed. I want to keep this as a default, but to be able to overwrite it with the users new choice.
I have added a user interface and a text box to capture the user's choice, and a installer class, but this is failing when I try it. Where it is failing is when it is try to find my App.Config file. It says that it cannot find it. In my code, I have written:
string path = Assembly.GetExecutingAssembly().Location;
So, to me my app.config has not been installed into this location when the installation is running at that moment in time.
So, can someone please advise how I can override and save the new location.
Thanks
What you're doing won't work for a number of reasons.
Custom actions in VS setup projects run after all the files have been installed, so it's too late to choose a folder where files can be installed. UI is supposed to be at the front where the normal wizard UI forms run, but VS setups don't support custom dialogs.
Installer classes with UI tend to do strange things because they are called from the execute sequence in the MSI, and the apartment threading won't work.
You're not running in an interactive user environment. Your installer class is being called from an msiexec process running with the system context. If you need to load files, name the path explicitly.
If you're going to stick with Visual Studio setups, your best bet is to install the script to a normal default location and have your app offer a way to copy it somewhere. Then the user can copy it somewhere new whenever they want and you're running in a normal user environment.
You can not get the directory full path where the MSI is being installed using,
string path = Assembly.GetExecutingAssembly().Location;
above code will probably return a path C:\Windows\System32 which has the msiexec.exe location which handles the MSI installation. Use the following code within installer class to get the installation path.
string installationPath = Context.Parameters["assemblypath"];
Rest of your questions are not clear.
Folks
Managed to resolve this myself.
I added an afterinstall event to the Installer class. In this event I added this line of code
string assembley = Assembly.GetExecutingAssembly().CodeBase;
UriBuilder uri = new UriBuilder(assembley);
string path = Uri.UnescapeDataString(uri.Path);
string directory = Path.GetDirectoryName(path);
To get the config file - I did this
Configuration config = ConfigurationManager.OpenExeConfiguration(Assembly.GetAssembly(typeof(ProjectInstaller)).Location);
This gives me what I want and it works

How to detect the install directory of a project?

I have a Visual studio C# project and I have an installer that installs the files into whatever directory the user specifies. I also have another installer with localized language resources and I want that to be installed in the aforementioned directory.
Can anyone point me in the right direction on this? I think I have to do something with registry keys but I don't know what to do.
Edit: For the record, I found this page: How to: Use a Registry Launch Condition to Specify a Target Directory. And I followed the instructions and it did what I wanted it to do.
Typically, you would create a registry subkey and value of HKEY_LOCAL_MACHINE\SOFTWARE\ and then just read that key back in your second installer to decide where to put that.
Something like HKLM\SOFTWARE\MyApplication,
Then you make a string value called InstallPath and write the path from your first installer there.
Depending on what you're doing, you may want to have a look at merge modules for installing several components with one MSI.
You are on the right track. Your first installer would write it's install path to a well known registry key. The second installer would read the path from that well known key and put it's dlls in the appropriate sub folders.
Here is one way to go at it:
Assembly a = Assembly.GetExecutingAssembly();
string folder = System.IO.Path.GetDirectoryName(a.CodeBase);

Categories

Resources