Visual Studio 2010 Automatic Attach To Process

后端 未结 10 2219
谎友^
谎友^ 2021-02-07 08:51

I am using visual studio 2010, my application has a multiu layer architect,

MainUI, WCFService, BLL and DAL

My MainUI communicated to WCF and WCF further commun

10条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-07 09:43

    Sample howto start a process and attach it to Visual Studio 2010 with EnvDTE(Version is relevant).

    //c:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\PublicAssemblies\EnvDTE.dll
    using Process = EnvDTE.Process;
    System.Diagnostics.Process p = new System.Diagnostics.Process();
    p.StartInfo.FileName = System.AppDomain.CurrentDomain.BaseDirectory + @"\YourProcess.exe";
    //Start the process
    p.Start();
    //Wait for process init
    System.Threading.Thread.Sleep(1000);
    
    bool attached = false;
    //did not find a better solution for this(since it's not super reliable)
    for (int i = 0; i < 5; i++)
    {
        if (attached)
        {
            break;
        }
        try
        {
            EnvDTE.DTE dte2 = (EnvDTE.DTE)System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE.10.0");
            EnvDTE.Debugger debugger = dte2.Debugger;
            foreach (Process program in debugger.LocalProcesses)
            {
                if (program.Name.Contains("YouProcess.exe"))
                {
                    program.Attach();
                    attached = true;
                }
            }
        }
        catch (Exception ex)
        {
            //handle execption...
        }
    }
    

提交回复
热议问题