locked
How to control VS 2010 from an external application via automation? RRS feed

  • Question

  • I have thousands of words I want to search for and replace in a large number of C files and so I'd like to write a WinForms application that uses Visual Studio 2010's automation capabilities to accomplish this. How can I do this?

    In particular:

    1) How to start and connect to the VS interface?

    2) How to navigate to a particular line?

    3) How to select the entire line?

    4) How to search within the line for a particular word, case sensitive?

    5) How to detect if the word is found?

    6) How to copy the entire line to the clipboard?

    7) How to replace the found word with another word?


    Richard Lewis Haggard
    Sunday, September 25, 2011 12:21 AM

Answers

All replies

  • While you can use VS automation for this I am not sure what it buys you.  Opening a file in an editor is a heavyweight undertaking due to language service integration, etc...  Since you wouldn't get any benefits from this you would be paying this cost for no reason.  It sounds like you are trying to do a simple search and replace some number of files, why not just use a Perl script or the .NET file streams + regular expressions?  I mean I can certainly instruct *how* to do what you want using VS, I just don't know if it is the best way to go about it.

    Ryan

    Sunday, September 25, 2011 4:59 PM
  • Please, let's not get sidetracked from the original question. 'Why' is not germain while 'how' is very much on topic. Are you aware of how to accomplish any of the 7 explicit goals specified in the original post and, if so, would it be possible for you to pass that information along to me? I'd appreciate it.


    Richard Lewis Haggard
    Tuesday, September 27, 2011 1:24 AM
  • Hi Richard,

    Please read this one: Referencing Automation Assemblies and the DTE2 Object (http://msdn.microsoft.com/en-us/library/t6d9sf9k.aspx), it is what you are looking for.

    I also suggest you read Discovering Code by Using the Code Model (http://msdn.microsoft.com/en-us/library/ms228763.aspx), which is talking about accessing the code model via DTE.

    Regards,

    Yi 


    Yi Feng Li [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, September 27, 2011 6:22 AM
  • Hello,

    Would you mind letting me know the result of the suggestions? If you need further assistance, feel free to let me know. I will be more than happy to be of assistance.

    Yi


    Yi Feng Li [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, September 30, 2011 1:42 AM
  • It can be done, but as Ryan already noted, this will be magnitudes slower, and take quite a bit of code and experimentation to implement. But it will most certainly give you a crash course in the VS automation framework, and COM interop with managed code (if you're looking to implement this in a managed language).

    The VS IDE is a COM automation server, and you can instantiate an instance of the IDE via CoCreateInstance and query for the DTE interface(use System.Activator.CreateInstance if using managed code).

    Once you have the DTE interface, you can open an existing project (if there is one), and then iterate through all the projects, iterate through all the project items (bearing in mind that each projectitem has a ProjectItems collection of it's own that may contain additonal source files.

    For each ProjectItem you find (ends with a particular extension like .cpp or .cs for example), you would call ProjectItem.Open to open the file with the code editor. The ProjectItem.Open method returns an EnvDTE.Window interface. Use this to retrive the EvDTE.TextDocument interface as described in Fengs post. (or Carlos' article)

    One you have the TextDocument interface, you can use the ReplacePattern method to change the text. Or you can leverage the TextSelection interface (there's probably a ton of searchable examples online).

    You will also likely need to factor in the fact that lengthy automation calls from a client application may time out, and require you to implement a custom IMessageFilter. I don't know if you'll hit this problem, but if you make a call to an automation method that takes a good amount of time to complete, your automation call will likely time out.

    Sincerely,


    Ed Dore
    Friday, September 30, 2011 3:01 AM
  • Excellent. I'm actually beginning to make progress on the series of tasks.

    Speed is not an issue.

    In this case the code that I will have to work with is not actually in a solution or a project. I'm going to have to open a series of standalone source files, navigate to a particular place and optionally do some things to it.

    1) How to start and connect to the VS interface?
    Add reference to DTE80.
    OK, I can gain access to EnvDTE80.DTE2 using
    EnvDTE80 dte2 = (EnvDTE80.DTE2)System.Runtime.InteropServices.Marshall.GetActiveObject("VisualStudio.DTE.10.0");

    2) How to open a particular file?
    dte2.ItemOperations.OpenFile( strFile );
    TextDocument objTextDoc = dte2.ActiveDocument.Object("TextDocument");

    3) How to navigate to a particular line?

    4) How to select the entire line?

    5) How to search within the line for a particular word, case sensitive?

    6) How to detect if the word is found?

    8) How to copy the entire line to the clipboard?

    9) How to replace the found word with another word?

    10) How to save the modified file?

    11) How to close the modified file?


    Richard Lewis Haggard
    Sunday, October 2, 2011 2:42 AM
  • Hi Richard,

    Please take a look How to: Automate Text Search and Replace and How to: Control the Code Editor about navigation and searching.

    For clipboard, you can use the Clipboard class from .Net framework, http://msdn.microsoft.com/en-us/library/system.windows.forms.clipboard.aspx

    For save and close the file, see Save method of Documenet class of DTE namespace, http://msdn.microsoft.com/en-us/library/envdte.document.save.aspx

    Hope it helps.

    Regards,

    Yi


    Yi Feng Li [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, October 3, 2011 3:01 AM