2

i want to write a macro to put selected 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.

Mehdi
  • 5,435
  • 6
  • 37
  • 57

1 Answers1

2

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.

mousio
  • 10,079
  • 4
  • 34
  • 43
  • thank you but i don't want to activate or view the target file. i want dynamically insert some text(for example an XML node as String to a XML file) and save the file without viewing, opening or activating it. is it possible to handle?! – Mehdi May 09 '11 at 07:58
  • Sure, though you did not mention that the file should not be opened in the editor: just code it using normal `System.IO.File` or `System.IO.Stream` calls and operations in your macro code… – mousio May 09 '11 at 08:12