I've tried wording it a few ways and can't find the answer I'm looking for so apologies if this has been answered before.

I've made basic web browser, calculator and notepad apps and would like them to open from one form. I can open them fine at the moment, but my code references the file path the apps are found in. How can I write my code so that these apps will open when transferred to another computer? I.e. not being able to find the file path. This is my code that's working so far:

private void button1_Click(object sender, EventArgs e)
{
    System.Diagnostics.Process.Start("C:\\Users\\Student User\\source\\repos\\Calculator2\\Calculator2\\bin\\debug\\Calculator2.exe");
}

private void button2_Click(object sender, EventArgs e)
{
    System.Diagnostics.Process.Start("C:\\Users\\Student User\\source\\repos\\WebAppUni1\\WebAppUni1\\bin\\Debug\\WebAppUni1.exe");
}

private void button3_Click(object sender, EventArgs e)
{
    System.Diagnostics.Process.Start("C:\\Users\\Student User\\source\\repos\\UniTextApp\\UniTextApp\\bin\\Debug\\UniTextApp.exe");
}

Would it just be a case of changing the file path to its new destination once ZIPped? Thanks!

3

There are 3 answers

2
CodeCaster On

What would stop someone from deploying the applications to different directories, or even disks? You can't know where they will install it, unless you provide an installer that properly installs and writes the installation location in the registry. And then you can read this location from the registry, and use it in your application.

So: provide an installer.

2
jcs On

How can I write my code so that these apps will open when transferred to another computer? I.e. not being able to find the file path.

If what you want is to access the same path, just changing the user's home directory, you can use %userprofile% in the path. Example:

%userprofile%\desktop
%userprofile%\documents

More options here: What is the alternative for ~ (user's home directory) on Windows command prompt?

0
farbiondriven On

There are some possibilities to get around this problem. Considered you don't want to locate the applications in the same folder structure of the main one,

1) You may replace the location string inside System.Diagnostics.Process.Start("C:\\Users\\Student User\\source\\repos\\UniTextApp\\UniTextApp\\bin\\Debug\\UniTextApp.exe"); with an actual variable whose value is initialised as you want. If it's not found in the first attempt, then you may use a browse form to lookup for the missing file. 2) Use an external settings file to set the location of the different applications.

If instead you can locate the applications in the same folder structure of the main one, the easiest approach - in my opinion - is to use relative paths.

e.g

..\\..\\..\\..\\UniTextApp\\UniTextApp\\bin\\Debug\\UniTextApp.exe