【问题标题】:Visual Studio 2010 EnvDTE.EditPoint Insert function returning 'Exception from HRESULT: 0x80041001'Visual Studio 2010 EnvDTE.EditPoint 插入函数返回“HRESULT 异常:0x80041001”
【发布时间】:2015-03-20 12:56:20
【问题描述】:

概述:

Microsoft Visual Studio 本身就是一个工具。双关语意。我正在尝试从用作 Visual Studio 插件的 System.Windows.Window 中使用 EnvDTE。我不想访问创建插件的项目,而是在使用窗口时当前打开的项目(完全不同的问题)。无论如何,我遇到了 EnvDTE.EditPoint.Insert() 方法的问题,每当我尝试使用它时,我都会收到异常'COM Exception was catched 'Exception from HRESULT: 0x80041001''。

这是我的代码:

public class DTEHelper
{
    static private DTE _dte = null;
    static private AddIn _addIn = null;
    static private ProjectItem _pItem = null;
    static private TextDocument _tDoc = null;
    static private EditPoint _ePoint = null;
    static private Project _projP = null;
    static private SolutionBuild _sBuild = null;

    public DTE dte { get { return _dte; } set { _dte = value; } }
    public AddIn addIn { get { return _addIn; } set { _addIn = value; } }
    public ProjectItem pItem { get { return _pItem; } set { _pItem = value; } }
    public TextDocument tDoc { get { return _tDoc; } set { _tDoc = value; } }
    public EditPoint ePoint { get { return _ePoint; } set { _ePoint = value; } }
    public Project projP { get { return _projP; } set { _projP = value; } }
    public SolutionBuild sBuild { get { return _sBuild; } set { _sBuild = value; } }
}

问题方法:

    private void addMethod(string file, DTEHelper dteHelper)
    {
        try
        {
            //Open existing file 
            System.Array projectFiles = (System.Array)dteHelper.dte.ActiveSolutionProjects;
            EnvDTE.Project project = null;
            if (projectFiles.Length > 0)
            {
                project = (EnvDTE.Project)(projectFiles.GetValue(0));

                dteHelper.pItem = (from ProjectItem csFile in project.ProjectItems where csFile.Name.Contains(file) select csFile).First();

                if (dteHelper.pItem.Name.Contains(".cs"))
                {
                    EnvDTE.Window window = dteHelper.pItem.Open(Constants.vsViewKindCode);
                    TextDocument document = (TextDocument)window.Document.Object("TextDocument");
                    EditPoint edit = (EditPoint)document.CreateEditPoint();
                    edit.EndOfDocument();
                    edit.LineUp();
                    edit.Insert(""); //Problem Right Here

                    string fileName = dteHelper.pItem.get_FileNames(0);
                    dteHelper.pItem.SaveAs(fileName);
                }
            }
        }
        catch (Exception e)
        {
            MessageBox.Show(string.Format(System.Globalization.CultureInfo.CurrentUICulture, "Failed to add code to: " + file, this.ToString()),
                "ERROR");
        }
    }

在窗口中按下按钮时初始化dteHelper.dte

        dteHelper.dte = (EnvDTE.DTE)System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE.10.0"); 

【问题讨论】:

    标签: c# .net visual-studio-2010 visual-studio


    【解决方案1】:

    结果一切正常,唯一的问题是我正在搜索的文件('foo.cs')不在项目中(编译器无法识别),所以我必须将它添加到解决方案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-13
      • 1970-01-01
      • 1970-01-01
      • 2011-05-27
      相关资源
      最近更新 更多