Получение кода из всех (закрытых) файлов в решении в Visual Studio SDK

Я пытаюсь получить и редактировать код всех html-файлов в проекте

я нашел способ перебрать все 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
                }
            }
        }
    }
}

Теперь я не могу получить код файлов, которые не открыты в окне редактора.

Как мне получить и отредактировать код "не открытых" ProjectItems?
как определить, является ли файл файлом кода? (например: .cs, .html, .htm, .asp, ....

0 ответов

Вы должны открыть ProjectItem, который хотите прочитать или отредактировать.

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

если вы не открываете ProjectItem anyItem.Doument нулевой.

Заметка: selection.Insert("") можно использовать для изменения кода

Другие вопросы по тегам