I have included an executable in my project ( i.e. when you unfold the project in the Solution Explorer in VS2008, you see it along with all the other classes). This executable will need to be launched by my program. How can I retrieve its path programmatically?
Here's an example of getting text from a text resource:
Assembly assembly = Assembly.GetExecutingAssembly();
Stream stream = assembly.GetManifestResourceStream("ResourceTextFileName");
StreamReader textStreamReader = new StreamReader( stream );
Hopefully you can glean how to do an executable with this.
You can use the Assembly
class -
Assembly.GetExecutingAssembly().Location
should point you towards your running app. Use that path, strip off the .EXE and add the filename of your executable.
string filename = Assembly.GetExecutingAssembly().Location;
filename = filename.Substring(0, filename.LastIndexOf("\\") + 1);
filename += "my_exe_name.exe";
Process.Start(filename);
Note that this won't work for webapps, however, as the Location property may point at random places like the GAC.
If you deploy the executable together with your project to the same folder, you can use the Environment class to get the working directory of your executable. That would be: Environment.CurrentDirectory.
You can set the "Copy to output directory" property of the executable in the solution explorer to "Copy allways" or "Copy if newer" to accoumplish this.
I've never done this before with executables... However, if it's an embedded resource, you can enumerate through the list of embedded resources in your application (or refer directly to it by name), when you find the one appropriate, write it to disk, and then execute it.
To extract:
Assembly assembly = Assembly.GetExecutingAssembly();
using (Stream stream = assembly.GetManifestResourceStream("resource name")
{
using FileStream fs= new FileStream("executable name")
{
byte[] buffer = new byte[32*1024];
int bytesRead;
while ((bytesRead= stream.Read(buffer, 0, buffer.Length)) > 0)
{
fs.Write(buffer, 0, bytesRead);
}
}
}
To execute:
using System.Diagnostics;
Process someProc;
someProc= Process.Start("executable name");
As Daniel L points out, don't forget to mark the resources as an "embedded resource".
This is not exactly what you asked, but could be useful when using one of the suggestions other people have posted:
Be aware that if your application is a Windows forms app, and it ends up installed in C:\Program Files... you might not have write access to your application folder if the user that starts the application is not running as administrator.
That could be a problem if you want to 'extract' the embedded resource and save it to disk in the same folder as your app.
I would recommend either:
Not embedding the .exe as a resource. Just set its Copy to Output Directory to Always. Then deploy it together with your main .exe
Embedding it as a resource but when saving it to disk, use either the Windows temp folder (if it is just a one time thing) or the User's data folder (C:\Documents and Settings\[user]\Application Data)
You can get the proper folder for the current OS like this:
// for the Windows temp folder
Path.GetTempPath()
// for the user's application data folder
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData))