提问人:Jabu 提问时间:8/21/2023 更新时间:8/21/2023 访问量:119
如何检测待保存的文件?
How to detect files pending save?
问:
我正在尝试遵循谢尔盖对同一问题的评论,即如何检测具有“待处理保存状态”的文件。
法典:
using EnvDTE;
using EnvDTE80;
using Microsoft.VisualStudio.Shell;
namespace Link
{
public static class ProjectHelpers
{
static ProjectHelpers()
{
// Rely on caller being on UI thread as shouldn't do it here in the constructor.
#pragma warning disable VSTHRD010 // Invoke single-threaded types on Main thread
Dte = (DTE)Package.GetGlobalService(typeof(DTE));
#pragma warning restore VSTHRD010 // Invoke single-threaded types on Main thread
Dte2 = (DTE2)Package.GetGlobalService(typeof(DTE));
}
public static DTE Dte { get; }
public static DTE2 Dte2 { get; }
}
}
然后我将 Dte.Documents 称为:
try
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
// Enumerate through open documents
foreach (Document doc in ProjectHelpers.Dte.Documents)
{
// Check if the document has unsaved changes
if (!doc.Saved)
{
Console.WriteLine($"Unsaved changes in document: {doc.FullName}");
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
但是我得到了这个例外:
The data necessary to complete this operation is not yet available. (Exception from HRESULT: 0x8000000A)
我错过了什么?
答: 暂无答案
评论
ProjectHelpers
ProjectHelpers.Dte.ActiveDocument
.Documents
dte