Partilhar via


How to Deploy and Use Executable Files with VSTO Add-ins?

I recently came across an interesting scenario: it is desired to deploy an executable file with a VSTO add-in and run the executable within the add-in code.

Well, at the first glance, this seemed to be a usual ClickOnce deployment which I had done before with different .NET applications many times: dealing with projects with more than one executables, I just needed to set the main project as “Startup Project”, include the second exe in the main project, and call the second exe from the main project using the Process.Start. I was never worried about the path to the second exe since it gets deployed next to the main one, so I always used relative path.

However, it is different when you try it with VSTO add-ins. The following code in my add-in startup could not find the exe file, despite the fact that “MySecondExe.exe” is deployed correctly:

Process.Start("MySecondExe.exe");

So, why the file could not be found? If you retrieved the value of the Current Directory in your add-in project and a WinForm project deployed through ClickOnce, the former returns something like “C:\Users\hamed\Documents”, and the latter returns the ClickOnce cache path. There you go! The base working directory is not what you would expect in add-in case. Easy to explain though, add-in is not an executable, it is a package of a manifest, assemblies, resources and other files such as data files. The manifest is parsed by the VSTOLoader, an AppDomain is created, and the add-in assembly gets loaded into the AppDomain. So we need to retrieve the assembly location, i.e. the ClickOnce cache path in which the add-in is deployed to, using the following statement:

string assemblyPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

You can then use the above path to locate your second exe file.

Good day!