检查未保存的更改并要求用户保存

Check for unsaved changes and ask user to save

我正在编写一个扩展程序,将文件添加到解决方案中的项目。

首先检查未保存的更改是有意义的,如果有,请用户保存。

我怎样才能做到这一点?

是否有任何开源 VS 扩展可以执行此操作,以便我看看它是如何完成的?

要查看是否有未保存的更改,您可以使用 DTE.Documents and check each one for the Document.Saved 属性.

枚举打开的文档

您可以尝试通过具有相应功能的Microsoft.Build.Evaluation来使用项目。

Extension which adds files to projects in the solution.

将新文件包含到项目后,必须重新评估它们。关注 Project.IsDirty 属性:

A project is modified and must be reevaluated when the underlying project source code, including changes to imported files. A project is also modified must be reevaluated if the project tool set changes.


Project.IsDirty | Project.ReevaluateIfNecessary | Project.Save