0

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!

GSerg
  • 76,472
  • 17
  • 159
  • 346
  • Apparently you have some way of deploying these four applications as a single bundle. What would that be? – GSerg Jan 08 '20 at 11:30
  • I don't know I'm very new to coding, this is my first uni project and had not coded previously. Do you know? – Successful-Standard Jan 08 '20 at 11:32
  • 1
    Check this [post](https://stackoverflow.com/questions/6041332/best-way-to-get-application-folder-path) – Kuba Do Jan 08 '20 at 11:46
  • So when you say "zip files", you mean that you open `C:\Users\Student User\source\repos` and zip the entire `Calculator2`, `WebAppUni1`, `UniTextApp` folders into a single archive? – GSerg Jan 08 '20 at 12:58
  • No, I have a console form with buttons to open each of the three apps. I have to ZIP the files to give to my lecturer who will open them on a separate machine so the file paths will change. I need to do something so the apps will still open and operate normally from the console form when opened on a different machine. – Successful-Standard Jan 08 '20 at 14:02
  • 1
    Which "the files"? You manually single out the four executables and zip them, and then the user extracts them to the same folder? Then `System.Diagnostics.Process.Start("Calculator2.exe")`. – GSerg Jan 08 '20 at 16:11
  • `System.Diagnostics.Process.Start(System.IO.Path.Combine("Calculator2", "Calculator2.exe")); ` ended up being what I had to use even when all .exe files were in the console/launcher form's debug folder. Thanks though! – Successful-Standard Jan 09 '20 at 13:58

3 Answers3

0

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.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • Okay I've no idea how to go about that so have some reading to do I guess. – Successful-Standard Jan 08 '20 at 11:47
  • That isn't at all what I wanted to do. I ended up putting all the .exe files of the apps in the launcher form's debug folder and used `System.Diagnostics.Process.Start(System.IO.Path.Combine("Calculator2", "Calculator2.exe"));` – Successful-Standard Jan 09 '20 at 13:59
0

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?

jcs
  • 611
  • 2
  • 9
  • 22
  • No they're going to be ZIPped and opened from a memory stick so the path will be nothing alike, this is the problem I have. – Successful-Standard Jan 08 '20 at 12:18
  • In this case, only way to be sure is ask the user for the path when your program starts or scan all the removable storage devices for the name of the zipped files. – jcs Jan 08 '20 at 13:57
0

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
farbiondriven
  • 2,450
  • 2
  • 15
  • 31