Search code examples
c#wpfvisual-studiolaunch

How To Use A WPF Window To Launch An Application From Desktop Folder On Any Machine


I keep searching the Internet for some sort of tutorial on this but none I've fount make it very clear. I've created a WPF Window in Visual Studio that has two Buttons Button One Is to be used to launch a PDF File. Button Two is to be used to launch the .exe application and then close the WPF window Now I find that if I use:

System.Diagnostics.Process.Start("C:/Users/mdkgr/Desktop/FolderName/app.exe")

Then it works, however, naturally when I publish this project out and people are to use it on their machine this method will not work because the address will no doubt be different for every machine it's installed on. My question is simply: How do I make this so it will launch either the PDF file or application.exe on any machine it's installed on? None of the Visual Studio tutorial I've seen make this very clear.

Edit:

I tried @bwing ' s idea but I get an error so I'm not sure if I'm using it right?? Here is my code in the public voids:

`public static void LaunchKOS()
        {
            var desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            // Launch Voice Server
            //Process.Start("C:/Users/mdkgr/Desktop/Knight O S Beta01_Data/voice-recognition-server-pc/VoiceServer/bin/Release/KittVoiceServer");

            var combinedPath = Path.Combine(desktopPath, "Knight O S Beta01_Data/voice-recognition-server-pc/VoiceServer/bin/Release/KittVoiceServer");
            Process.Start(combinedPath);

            // Launch Knight O.S. Unity Settings Window
            Process.Start("C:/Users/mdkgr/Desktop/Knight O S Beta01");
            // Close This Window
            Environment.Exit(0);
        }
        public static void LaunchPDF()
        {
            Process.Start("C:/Users/mdkgr/Desktop/Knight O S Beta01_Data/Knight O.S. Features.pdf");
        }`

EDIT:

This method is working just fine on one of my machines but as soon as I try it on my HP laptop it only wants to open the Unity3d application. and yet on both machines the folder directories are exactly the same and I can manually open each .exe files with no problems but my launcher application only wants to open the one .exe file on my HP laptop. Does anyone have any clue why this would be happening?

public static void LaunchKOS()
        {
            

            var desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            var desktopPathkos = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            // Launch Voice Server
            var combinedPath = Path.Combine(desktopPath, "Knight O S Beta01_Data/voice-recognition-server-pc/VoiceServer/bin/Release/KittVoiceServer");
            Process.Start(combinedPath);
            // Launch Knight O.S. Unity Settings Window
            var combinedPathkos = Path.Combine(desktopPathkos, "Knight O S Beta01");
            Process.Start(combinedPathkos);

            
            // Close This Window
            Environment.Exit(0);
        }


Solution

  • If the files are on the desktop, you can get the path to the desktop with

    var desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
    

    If the files are in the same folder as the WPF Window application you can use this to get that folder

    var exePath = AppDomain.CurrentDomain.BaseDirectory;
    

    Then you just append the file name and make the call as you were before

    var combinedPath = Path.Combine(desktopPath, "app.exe");
    Process.Start(combinedPath