Sukhdevsinh Zala
Sukhdevsinh Zala

Reputation: 1215

how to invoke visual studio commands from c#

Can i execute Visual Studio commands like 'File.AddExistingProject' from C# code? if so how?

other sample commands..

Debug.Print varA File.CheckOutforEdit etc.

Upvotes: 1

Views: 460

Answers (1)

pushpraj
pushpraj

Reputation: 13669

You can make use of DTE for the same, I have done something similar in past here is my code to do the same

DTE dte = null;

try
{
    dte = (DTE)Marshal.GetActiveObject("VisualStudio.DTE.11.0");
}
catch
{    
    System.Diagnostics.Process.Start("devenv");
    System.Threading.Thread.Sleep(2000);
    try
    {
        dte = (DTE)Marshal.GetActiveObject("VisualStudio.DTE.11.0");
    }
    catch
    {
        //cant do much nw, notify
    }
}

SearchResult result = (SearchResult)listFiles.SelectedItem;

dte.ExecuteCommand("File.OpenFile", result.FileName);
System.Threading.Thread.Sleep(200);
dte.ExecuteCommand("Edit.GoTo", labelLineNo.Text);
dte.MainWindow.Activate();
dte.ActiveDocument.Activate();

I hope this helps you

so you may call

dte.ExecuteCommand("File.AddExistingProject", parameters as needed);

Upvotes: 4

Related Questions