1

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

other sample commands..

Debug.Print varA File.CheckOutforEdit etc.

0

1 Answer 1

4

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);
Sign up to request clarification or add additional context in comments.

5 Comments

thanks for the ans..have one more question...is there a command to open "Add Connection" dialog box.??
I am not sure which connection you are referring to, you may try to figure out commands by typing > in immediate window followed by the expected command. Alternatively you can make use of Find/Command box to find the same. see more msdn.microsoft.com/en-us/library/1665hyw1(v=vs.80).aspx
Note: For Visual Studio 2013 (or any version other than 2012) you'll need to change the VisualStudio.DTE.11.0 accordingly. 2013 it would be "VisualStudio.DTE.12.0".
@Pushpraj Ruhal - Thread.Sleep(2000)? I wish my Visual Studio loaded in 2 seconds!
@ppittle Actually this snippet is from one of my tool I made and 2000 ms was implemented as a safe side to make sure the VS is loaded and ready for receiving commands if not already running. BTW VS usually loads in approx a sec on my pc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.