如何使用 DTE 编辑加载项项目中的 .cs 文件

发布于 2024-10-27 19:37:48 字数 363 浏览 4 评论 0原文

我正在尝试为 vs2010 编写我的第一个插件,但我很挣扎。

我有一个生成大量 cs 文件的程序集。我希望我的插件将新文件添加到所选项目中,或者如果文件存在,则覆盖它们。

我遇到两个问题:

  1. 当我添加新文件时,如何将其添加到项目内的子文件夹中?我似乎只能添加到项目的根目录。
  2. 如果cs文件存在,如何清除其内容?我使用 EnvDTE.TextDocument & EnvDTE.EditPoint 接口。但每次我尝试迭代文档清除行时,都会收到 COM 错误“来自 HRESULT 的异常:0x80041001”。

    如果可以的话,我不想删除该文件并添加新文件。由于源代码管理上的日志记录。

I'm trying to write my first add-in for vs2010, but im struggling.

I have a assembly that generates lots of cs files. I want my plugin to add new files to the select project or if the files exist, overwrite them.

I'm having 2 problems:

  1. When I add a new file, how do I add it to a sub folder inside the project? I seem to only be able to add to the root of the project.
  2. If a cs file exists, how do I clear its content? Im using the EnvDTE.TextDocument & EnvDTE.EditPoint interfaces. But every time I try and iterate through the document clearing lines, I get a COM error "Exception from HRESULT: 0x80041001".

    I dont want to delete the file and add a new file if I can help it. Due to the logging on source control.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

季末如歌 2024-11-03 19:37:48
textDoc = (TextDocument) document.Object("TextDocument");
EditPoint editPoint = (EditPoint)textDoc.StartPoint.CreateEditPoint();
EditPoint endPoint = (EditPoint)textDoc.EndPoint.CreateEditPoint();
editPoint.Delete(endPoint);

不需要循环,并且您的编辑点永远不会从第一个位置移动。

textDoc = (TextDocument) document.Object("TextDocument");
EditPoint editPoint = (EditPoint)textDoc.StartPoint.CreateEditPoint();
EditPoint endPoint = (EditPoint)textDoc.EndPoint.CreateEditPoint();
editPoint.Delete(endPoint);

No looping needed and your editpoint never moves from the first position.

与他有关 2024-11-03 19:37:48

好吧,我有一种方法可以完成这项工作。

 
// Get an instance of the currently running Visual Studio IDE.
var dte2 = (EnvDTE80.DTE2)System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE.10.0");

//I store the list of projects in dte2.Solution.Projects in a combobox

EnvDTE.Project project = (EnvDTE.Project)projectList.SelectedValue; //I get my projects out of a combobox

foreach (ProjectItem projectItem in project.ProjectItems)
{
    Document document;
    try
    {
        projectItem.Open();
        document = projectItem.Document;
    }
    catch(Exception)
    {
        Console.WriteLine("failed to load document");
        continue;
    }
    if (document == null)
    {
        continue;
    } 

    if (document.Name == "Class1.cs") //whatever file your after
    {
        TextDocument editDoc = (TextDocument) document.Object("TextDocument");
        EditPoint objEditPt = editDoc.CreateEditPoint();
        objEditPt.StartOfDocument();
        document.ReadOnly = false;

        while (!objEditPt.AtEndOfDocument)
        {           
            objEditPt.Delete(objEditPt.LineLength); 
            objEditPt.LineDown(1);
        }

        objEditPt.DeleteWhitespace(vsWhitespaceOptions.vsWhitespaceOptionsHorizontal);
        objEditPt.DeleteWhitespace(vsWhitespaceOptions.vsWhitespaceOptionsVertical);

        Console.WriteLine("saving file {0}", document.FullName);
        document.Save(document.FullName);   
    }
}

Well i've got a one way of doing this working.

 
// Get an instance of the currently running Visual Studio IDE.
var dte2 = (EnvDTE80.DTE2)System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE.10.0");

//I store the list of projects in dte2.Solution.Projects in a combobox

EnvDTE.Project project = (EnvDTE.Project)projectList.SelectedValue; //I get my projects out of a combobox

foreach (ProjectItem projectItem in project.ProjectItems)
{
    Document document;
    try
    {
        projectItem.Open();
        document = projectItem.Document;
    }
    catch(Exception)
    {
        Console.WriteLine("failed to load document");
        continue;
    }
    if (document == null)
    {
        continue;
    } 

    if (document.Name == "Class1.cs") //whatever file your after
    {
        TextDocument editDoc = (TextDocument) document.Object("TextDocument");
        EditPoint objEditPt = editDoc.CreateEditPoint();
        objEditPt.StartOfDocument();
        document.ReadOnly = false;

        while (!objEditPt.AtEndOfDocument)
        {           
            objEditPt.Delete(objEditPt.LineLength); 
            objEditPt.LineDown(1);
        }

        objEditPt.DeleteWhitespace(vsWhitespaceOptions.vsWhitespaceOptionsHorizontal);
        objEditPt.DeleteWhitespace(vsWhitespaceOptions.vsWhitespaceOptionsVertical);

        Console.WriteLine("saving file {0}", document.FullName);
        document.Save(document.FullName);   
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文