Вопрос

I have a project that generates text (representing an interface and a class) based on metadata. I would like to take this generated code and insert it as a new class and interface directly into the currently opened solution under a specific project and directory. I will create the menu tool that will generate the class but what I don't know how to do is gain access to the following items from within my custom Visual Studio Extension:

  1. Iterate the current solution and find a project to dump the generated code into.
  2. Open a new file window within Visual Studio and inject the generated text that comes from my tool directly into that window.
  3. Create a new folder in a specific project within the current solution from within my custom extension.

EDIT - To clarify I need to open a new file (e.g. Right Click on a Project -> Add - > New Class) and insert text into it from within my custom Visual Studio Extension.

Thanks

Это было полезно?

Решение

For creating a new File from a Visual Studio Extension (ToolWindowPane) first use the GetService method:

// Get an instance of the currently running Visual Studio IDE
DTE dte = (DTE)GetService(typeof(DTE));

Second, ensure that a Solution is currently open, if no solution is open the File generation will not work:

string solutionDir = System.IO.Path.GetDirectoryName(dte.Solution.FullName);

Third, generate the new file from the DTE object:

dte.ItemOperations.NewFile(@"General\Visual C# Class", "ObjectOne", EnvDTE.Constants.vsViewKindTextView);

After creating the new file use the following code to access the text of that file and replace it with your generated text:

TextSelection txtSel = (TextSelection)dte.ActiveDocument.Selection;
TextDocument txtDoc = (TextDocument)dte.ActiveDocument.Object("");

txtSel.SelectAll();
txtSel.Delete();
txtSel.Insert("Hello World");
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top