H0ckeyfr33k99 H0ckeyfr33k99 - 1 month ago 7
C# Question

Calling a resource installed in the user's directory

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!

Answer

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.