我正在尝试为vs2010编写我的第一个加载项,但我正在努力。
我有一个生成大量cs文件的程序集。我希望我的插件将新文件添加到选择项目中,或者如果文件存在,则覆盖它们。
我遇到两个问题:
我不想删除该文件并添加新文件,如果我可以帮助它。由于登录源控件。
答案 0 :(得分:6)
textDoc = (TextDocument) document.Object("TextDocument");
EditPoint editPoint = (EditPoint)textDoc.StartPoint.CreateEditPoint();
EditPoint endPoint = (EditPoint)textDoc.EndPoint.CreateEditPoint();
editPoint.Delete(endPoint);
不需要循环,您的编辑点永远不会从第一个位置移动。
答案 1 :(得分:3)
我有一种方法可以做到这一点。
// 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);
}
}