Running vshost.exe as a different user

106 views Asked by At

Can I start appname.vshost.exe from debug folder under different username than the one used to start VisualStudio?

There is appname.vshost.exe.config with the following content. Is there a config for username? I have tried searching for it but couldn't find anything.

<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
    </startup>
</configuration>
2

There are 2 answers

0
Alpesh On BEST ANSWER

I don't think you can start vshost.exe under different user than the one you have used to start Visual Studio. So now I am starting main console app under different user from another console app and attaching debugger to it and it works.

I have copied my code below if it helps anyone.

using System;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security;
using EnvDTE80;
using Process = System.Diagnostics.Process;

namespace StartService
{
    class Program
    {
        static void Main(string[] args)
        {
            var secure = new SecureString();
            foreach (var c in "password-from-config")
            {
                secure.AppendChar(c);
            }

            Process process = null;

            try
            {
                process = Process.Start(@"C:\Test Projects\WcfServiceTest\WcfServiceTest\bin\Debug\WcfServiceTest.exe",
                    "TestUser", secure, "DomainName");

                Attach(GetCurrent());

                Console.ReadKey();
            }
            finally
            {
                if (process != null && !process.HasExited)
                {
                    process.CloseMainWindow();
                    process.Close();
                }    
            }
        }

        public static void Attach(DTE2 dte)
        {
            var processes = dte.Debugger.LocalProcesses;
            foreach (var proc in processes.Cast<EnvDTE.Process>().Where(proc => proc.Name.IndexOf("WcfServiceTest.exe") != -1))
                proc.Attach();
        }

        internal static DTE2 GetCurrent()
        {
            var dte2 = (DTE2)Marshal.GetActiveObject("VisualStudio.DTE.12.0"); // Specific to VS2013

            return dte2;
        }
    }
}
3
Arukaito On

If you're trying to run your debugg executable You can try shift right click and Run as different user.

Or do you want to run as different user via configuration?