Getting code from all (closed) files in solution in Visual Studio SDK

430 views Asked by At

I'm trying to get and edit the code of all the html-files in the project

i found a way to loop over all ProjectItems

IEnumerator Projects = _applicationObject.Solution.Projects.GetEnumerator();

while (Projects.MoveNext())
{
    IEnumerator Items = ((Project)Projects.Current).ProjectItems.GetEnumerator();
    Queue<ProjectItem> ProjectItems = new Queue<ProjectItem>();
    while (Items.MoveNext())
    {
        ProjectItem SubItem = (ProjectItem)Items.Current;
        try
        {
            if (SubItem.Document != null) DocumentIndex.Add(SubItem.Document);
        }
        catch (Exception Exception)
        {
            Console.WriteLine(Exception.Message);
            //ignore
        }
        ProjectItems.Enqueue(SubItem);
    }

    //follow the tree down
    while (ProjectItems.Count != 0)
    {
        ProjectItem ProjectItem = ProjectItems.Dequeue();
        if (ProjectItem.ProjectItems != null)
        {
            foreach (ProjectItem SubItem in ProjectItem.ProjectItems)
            {
                ProjectItems.Enqueue(SubItem);
                try
                {
                    try
                    {
                        SubItem.Open(SubItem.Kind);
                        DocumentIndex.Add(SubItem.Document);
                    }catch(Exception Ex){
                        Console.WriteLine(Ex.Message);
                    }
                }
                catch (Exception Exception)
                {
                    Console.WriteLine(Exception.Message);
                    //ignore
                }
            }
        }
    }
}

now i can't get to the code of the files that are not open in an editor window.

how do i get and edit the code of "not opened" projectItems?
how do i detect if a file is a code file? (eg: .cs, .html, .htm, .asp, ....

1

There are 1 answers

0
denifer santiago fernandez On

You must open the ProjectItem that you want to read or edit

DTE dte = (DTE)Package.GetGlobalService(typeof(DTE));
var project = dte.Solution.Projects.Item(1);
var projectItems = project.ProjectItems;
var anyItem = projectItems.Item(0);
Window anyItemWindow = anyItem.open()
var selection = anyItem.Document.Selection as TextSelection;
selection.SelectAll();
Console.WriteLine(selection.Text) // All code
anyItem.Document.Close() //Close Document

if you don't open the ProjectItem anyItem.Doument is null.

Note: selection.Insert("") can be used to change the code