.NET/C#: obtaining the full path of the process without the “.vshost” part.
Posted by jpluimers on 2013/10/08
When debugging from Visual Studio, the actual process is often the .vshost.exe one.
Visual Studio 2005 and up use to the .vshost.exe to expedite the debugging sequence startup, and enables some debugger and design-time features. This is also the reason why your bin directory is locked as soon as you open a project in Visual Studio.
You can disable this feature, but then starting the debugging sequence gets a lot slower, and you loose some security and design-time expression evaluation.
Often (for instance when your process needs to start itself multiple times) you want to have the name without the .vshost part.
There are also circumstances, where the encompassing process is not a .NET one but a native process (for instance when running under IIS, or when running an Office Add-In).
So I wrote an AssemblyHelper class that retrieves some key properties of the currently running process, be it a .NET assembly or a native process.
Part of the code was inspired by the original Delphi .NET (not Prism) run-time library.
A few notes:
- The System.AppDomain.CurrentDomain has a FriendlyName property (no, there ain’t ScaryName or StarWarsName) which doesn’t always work as it can be set to anything.
- The FileName property of the System.Diagnostics.Process.GetCurrentProcess() gives you the full path name, which can potentially include “.vshost”.
- The various Visual Studio 2008 flavours of vshost.exe are in the directory shown below. Ajust the version number for more recent Visual Studio versions:
%ProgramFiles%\Microsoft Visual Studio 9.0\Common7\IDE
21-12-2009 11:40 34.648 vsdiag_regwcf.exe
21-12-2009 11:40 11.592 vshost.exe
21-12-2009 11:40 11.600 vshost32.exe
21-12-2009 11:40 439.624 vsta.exe
vshost.exe basically contains the below code in the Microsoft.VisualStudio.HostingProcess namespace, which you can use to check if your code is being called from the immediate window.
namespace Microsoft.VisualStudio.HostingProcess
{
public sealed class EntryPoint
{
// Methods
private EntryPoint()
{
}
[DebuggerNonUserCode]
public static void Main()
{
if (Synchronize.HostingProcessInitialized != null)
{
Synchronize.HostingProcessInitialized.Set();
if (Synchronize.StartRunningUsersAssembly != null)
{
Synchronize.StartRunningUsersAssembly.WaitOne();
}
}
}
}
}
Some interesting reads that helped me getting this code to work:
- c# – What is the purpose of vshost.exe file? – Stack Overflow.
- fileversioninfo – How do I get the .exe name of a C# console application? – Stack Overflow.
- Debugging and the Hosting Process.
- Hosting Process (vshost.exe).
- VSHOST — the Hosting Process – dtemp’s WebLog – Site Home – MSDN Blogs.
- ProcessModule.FileName Property (System.Diagnostics).
- c# – How can I tell if my code has been called from the Immediate Window? – Stack Overflow.
–jeroen






Leave a comment