1

I'm trying to find out a way to run a CMD process in my WPF application without the need to write the CMD application to the user's temp directory.

The application I have created is essentially a wrapper to execute another CMD application and the intent is as follows:

  1. Install the application to the user's Program Files directory.
    • The directory that the app installs would also have a folder labeled something like "resources" that would hold the CMD application (upgrade.exe) and a file that the CMD application requires (image.bin).
  2. Run the application either from a shortcut on the desktop or from the Start menu.
  3. When the application needs to, call the upgrade.exe CMD application from the Program Files location it resides ("C:\Program Files\appname\resources\upgrade.exe").

What I have right now is code that writes the file to the user's temp directory and then runs it from there (code below). Both of the files are added to my solution as Resources to the project but if I need to do something different with them to use them, that's no problem. I don't know if there is a way to know exactly the location as I described it above but, if there is, how would I go about changing my code to work that way?

        path = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "upgrade.exe");
        File.WriteAllBytes(path, Properties.Resources.upgrade);
        imagepath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "image.bin");
        File.WriteAllBytes(imagepath, Properties.Resources.image);
        ProcessStartInfo processSetup = new ProcessStartInfo(path)
        {
            WorkingDirectory = System.IO.Path.GetTempPath(),
            Arguments = String.Format("-F -p {0} -f image.bin, UserComPort),
            UseShellExecute = false,
            CreateNoWindow = true,
            RedirectStandardError = true,
            RedirectStandardOutput = true
        };
        update = Process.Start(processSetup);

I don't love this method because it puts my files into a different location than where I would like them to reside permanently on the user's PC, even though I know I can delete them after I'm done using them.

Any help would be greatly appreciated.

EDIT: As indicated below, the correct solution was to use the AppDomain.CurrentDomain.BaseDirectory location and then append the known directory location to that path. You have to know where your resource is going to go in your program's directory structure to use this method (I used Inno Setup and defined the "Dir" that I wanted those resources to reside). The final solution is below:

        path = AppDomain.CurrentDomain.BaseDirectory + "resources\\upgrade.exe";
        imagepath = AppDomain.CurrentDomain.BaseDirectory + "resources\\image.bin";

This results in a much cleaner solution that does not require writing the file to a new location on the user's PC. Thank you to Phil N DeBlanc for this solution!

H0ckeyfr33k99
  • 145
  • 2
  • 4
  • 13

1 Answers1

1

If I understand what you're asking, I think you can find a solution here. You can get the current executing directory then append the necessary remaining path information to your CMD directory.

Community
  • 1
  • 1
Phil N DeBlanc
  • 398
  • 1
  • 13
  • I suppose this is the correct implementation. Basically I would extract the current directory (which would be the directory the user decided to install my application to) and then append "\resources\upgrade.exe" to the path to get to the file I want. I'll play around with this method to be sure but I'm pretty sure this is the right answer. Thanks a lot! – H0ckeyfr33k99 Nov 02 '16 at 20:26
  • This was a perfect implementation. I will edit my code with the solution. – H0ckeyfr33k99 Nov 03 '16 at 17:13
  • Glad to hear that! – Phil N DeBlanc Nov 03 '16 at 17:52