Document
objects are used to represent an open document in the IDE. To contrast this abstraction with that provided by the Window
object, a Window
object is used to represent the physical UI aspects of a document window, whereas a Document
object is used to represent the physical document that is being displayed within that document window.
A document could be a designer, such as the Windows Forms Designer, or it could be a text-based document such as a ReadMe file or a C# code file open in an editor.
Just as you get a list of all open windows using the DTE.Windows
collection, you can use the DTE.Documents
collection to retrieve a list of all open documents.
Dim documents As Documents = DTE.Documents
The Documents
collection is indexed by the document’s Name
property, which is, in effect, the document’s filename without the path information. This makes it easy to quickly retrieve a Document
instance.
Dim documents As Documents = DTE.Documents
Dim readme As Document = documents.Item("ReadMe.txt")
Documents documents = DTE.Documents;
Document readme = documents.Item["ReadMe.txt"];
Using the Document
object, you can do the following:
Close the document (and optionally save changes)
Retrieve the filename and path of the document
Determine whether the document has been modified since the last time it was saved
Determine what, if anything, is currently selected within the document
Obtain a ProjectItem
instance representing the project item that is associated with the document
Read and edit the contents of text documents
Table 14.11 contains the member descriptions for the Document
object.