1

我正在尝试获取和编辑项目中所有 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
                }
            }
        }
    }
}

现在我无法获取未在编辑器窗口中打开的文件的代码。

如何获取和编辑“未打开”项目项的代码?
如何检测文件是否为代码文件?(例如:.cs、.html、.htm、.asp、....

4

1 回答 1

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

如果您不打开 ProjectItemanyItem.Doument为空。

注:selection.Insert("")可用于更改代码

于 2020-10-25T23:19:05.220 回答