Archive

Archive for the ‘C#’ Category

Setting PATH while Installing an Application – Visual Studio 2005 Setup project

July 9th, 2008 Shyju 2 comments

A Small stuff to explain how to set the path variable for our executable’s Application folder.So that we can call the executable from anywhere in the PC ,(like invoking the notepad application from the PC,by command prompt).

Start……????????

Create a Project for Your Actual requirement.Write code for the functionalities u want to run when the user run the executable on the machine.Once You finish it,You can add a deployment Setup project to this project.for this Go to File->Add->New Project.Then a Dialog box will appear and from there we can select the Other Project types->Setup wizard.This Project will be added to the Available Project and Hence a Solution will be created and both of the projects will be comes under the Solution.

I hope you guys know how to Go ahead with the deployment creation wizard.very simple.Only thing u need to do is clicking the proper NEXT buttons !

Solution Explorer with 2 projects

Now When u build the Projects,You will get the Installable stuff.You can find this in your project’s(Deployment Setup project) Debug directory.The file name will be having an .msi extension.

Project Setup output

You can run this installer program in your pc and it will install our Application in it.

If you go to the Application folder ,You can see the executable there.

exe in progrm files

You can double click on the exe file to run it.

we can run the program from Commandprompt too.But for that we have to traverse through the folders using cd command.
Now we are going to make some changes to an environment variable for running our new executable.There is an environment variable called “Path” ,Where we will map some folder locations where the system has to search for some executables when a call for the exe comes.

for example,if we are typing notepad in command line, a new Notepad application will be launched.how it came ?Here comes the significance of “PATH”.The Full address of the notepad.exe has been mapped in the Path variable.so when the system get a call to the executable.It will look in to the Folders which are mapped in PATH.

Now we are going to Set the path for our exe in the system environment variable.
For that we will create another DLL and this dll will be called when the User installs our application

So we can add another project to our existing Solution.
File->Add->New Project
The newly added project too comes under solution explorer.Here we will add code to Set the path varaible value(our new appliacation folder location).In this we have to put the following code

// CODEpublic void SetPathVariable()
{
string[] cmd_args = System.Environment.GetCommandLineArgs();
AppendPathVariable(cmd_args[0]);
}
public void AppendPathVariable(string appPath)
{
try
{

string loc = GetPathOnly(appPath);
// System.Environment.SetEnvironmentVariable(“Path”, loc, EnvironmentVariableTarget.User);
string Value = System.Environment.GetEnvironmentVariable(“Path”, EnvironmentVariableTarget.Machine);
Value = Value + “;” + loc;
System.Environment.SetEnvironmentVariable(“Path”, Value, EnvironmentVariableTarget.Machine);

}
catch (Exception ex)
{
MessageBox.Show(“Omnex CRM found an Error in Setting the Environment variable\n” + ex.ToString());
}
}

// Function to Return the URL exclude the Application(exe) name
public string GetPathOnly(string url)
{
string outputStr = “”;
string ipurl = url;
char[] delimiter = { ‘\\’ };
string[] values = ipurl.Split(delimiter);
for (int i = 0; i < values.Length – 1; i++)
{
if (i == 0)
{
outputStr = values[i];
}
else
{
outputStr = outputStr + “\\” + values[i];
}
}
return outputStr;
}

private void Form1_Load(object sender, EventArgs e)
{
SetPathVariable();
this.Close();
}

//CODE ENDS

This part is over.from the code itself you would have understand what we are doing .
In the Form_Load ,we are calling the method SetPAthVariable().Let us check what is

inside SetPathvaraiable.we are reading the commandline arguments and passing the first
argument to another function called AppendPathVariable.Assume that the first argument
is having the full path to the exe with the executable name.Inside AppendPathVariable,we are caling GetPathOnly() for removing the executable name from the entire path.After that we are fetching the current Path variable then appending the New Path to It.Using the static method SetEnvironmentVariable(),we are setting the newly created path.

Now we have to Pass the Commandline arguments to this Program.For that we will use the custom action feature to our deployment Setup project
(Select the Project View->Editor->Custom Action).

Add Custom Action to Deployment Project

There will be for options such as Install,Commit,Rollback,UnInstall
Select Commit,Right click,Select Add custom action,Select the EXE where i have the code to set the path variable.

Add Custom Action
Click OK
Select the Newly added Exe from the list.Go to its properties,

Set the Installer class property to false.
Set the value of the ARGUMENTS property to

“[OriginalDatabase]<>[TARGETDIR]<>[ComputerName]“

custom Action Properties

Thats all here.
Then just build the Projects-Solution.Get the Latest msi files from the Debug folder of Deployment Setup project

Guys , try this .This was working for me.If you have any queries , Please let me know .Happy to help (with my little knowledge)….