将文件保存在 Visual Studio 2015 包中

Save file in Visual Studio 2015 package

我正在开发我的第一个 Visual Studio 扩展。我设法在保存文件时设置事件侦听器,并以这种方式使用 DocData 修改另一个文件:

var docData = new DocData(this, fileName);
var editorAdaptersFactoryService = (this.GetService(typeof(SComponentModel)) as IComponentModel).GetService<IVsEditorAdaptersFactoryService>();
var textBuffer = editorAdaptersFactoryService.GetDataBuffer(docData.Buffer);
textBuffer.Replace(new Span(0, textBuffer.CurrentSnapshot.Length), content);

这里的问题是,在我修改了这个新文件之后,我必须保存它,但是我找不到任何关于如何保存文件的例子。在我的 DocumentSaved 侦听器中,我有一个 EnvDTE.Document,我可以在其中调用 EnvDTE.Document.Save(),但我正在尝试保存另一个文件,我有我正在尝试保存的这个新文件的完整路径.我想遍历项目中的所有文件以查找我的特定文件,并在 ProjectItem 中使用可用的 Save() 方法。我尝试使用 this 答案中的代码,但没有用。

任何关于这方面的指导都会很棒!

你可以尝试使用完整路径

ProjectItem DTE.Solution.FindProjectItem("Fullpath")

https://msdn.microsoft.com/en-us/library/envdte._solution.findprojectitem.aspx