How to Program a Visual Studio 2010 Macro For accessing and editing specific Project files?
i want to write a macro to put sele开发者_开发问答cted text to a specific XML file in my project. for example my path is ~/Pages/Dictionary/en.xml. and i want to put selected text from an aspx.cs file to en.xml file.
please guide me from where I should start. i can get the selected text. now i don't know how can i access a file content goto end of file(or another place in file) and insert some text according to selected text to it.To open the file, either use the tree path in the solution explorer or just use the full file path:
DTE.Windows.Item(Constants.vsWindowKindSolutionExplorer).Activate()
DTE.ActiveWindow.Object.GetItem _
("{solutionname}\{projectname}\Pages\Dictionary\en.xml") _
.Select(vsUISelectionType.vsUISelectionTypeSelect)
DTE.ActiveWindow.Object.DoDefaultAction()
or
DTE.ItemOperations.OpenFile _
("{projectpath}\Pages\Dictionary\en.xml")
DTE.ActiveDocument.Activate()
You did not mention whether this is for a single project and/or solution, so I do not know if just hard coding the items in the curly braces will suffice.
To insert the text at the end of the file, you can select the end of the document and just paste (e.g. if you used Selection.Copy()
), or you can create an edit point and insert any text:
DTE.ActiveDocument.Selection.EndOfDocument()
DTE.ActiveDocument.Selection.Paste()
or
Dim editPoint As EnvDTE.EditPoint
selection = DTE.ActiveDocument.Selection()
editPoint = selection.TopPoint.CreateEditPoint()
editPoint.Insert("any text" + vbLf)
I am not sure if the end of the file is the real location where you want to add text; if not, one can navigate the document with e.g. StartOfLine()
, LineUp()
, WordRight()
, or other means to control the code editor.
精彩评论