c# - 操作方法 : Stop adding files to "Recent files" in Quick Access when opening/saving files with Microsoft. Office.Interop

标签 c# office-interop recent-file-list

使用以下代码打开和保存 Word/Excel 文档时,打开和保存的文件将添加到 Windows 文件资源管理器的“最近使用的文件”中(见屏幕截图)。代码本身可以很好地满足我的目的;我只复制了一小部分与环绕相关的代码。然而,我很难阻止这种不受欢迎的行为,互联网搜索似乎只给我关于如何防止文件出现在办公应用程序本身的“最近文件”列表中的结果,而不是 Windows 文件资源管理器。

我在包含上千个旧非 xml 格式的 Office 文件的目录上运行此代码,有些目录甚至分成 5 位数。当调用 .Open() 时,原始文件显示在列表中,当调用 .SaveAs()/.SaveAs2() 时新文件出现在列表中。当我单步执行代码时,这是实时发生的,它会导致 explorer.exe 进程的 CPU 使用率激增。打开和重新保存旧格式的 office 文件的 Action 发生得相当快,我怀疑由于 explorer.exe 不断处理最近的文件,这会导致大量 CPU 使用负载。我认为相关的其他症状是,当代码运行时,光标下方始终有旋转轮,并且整个操作系统 GUI 似乎变得有些无响应。

明确地说,我认为阻止 Windows 将文件添加到列表的主动解决方案将是最佳途径,而不是仅在事后清理列表的追溯解决方案。

using WORD = Microsoft.Office.Interop.Word;
using EXCEL = Microsoft.Office.Interop.Excel;

//==============================================================================


try
{
    if (newFileExt == FileExt.NEW_Word)
    {
        //open the doc
        var document = WordApp.Documents.Open(FileName: fdesc.FileInfo.FullName, ConfirmConversions: false, ReadOnly: true, AddToRecentFiles: false, Visible: false);

        //save the doc
        document.SaveAs2(FileName: newname, FileFormat: WORD.WdSaveFormat.wdFormatXMLDocument, CompatibilityMode: WORD.WdCompatibilityMode.wdCurrent, AddToRecentFiles: false);

        // close the doc
        document.Close(WORD.WdSaveOptions.wdDoNotSaveChanges);
    }
    else if (newFileExt == FileExt.NEW_Excel)
    {
        // open the workbook
        /// https://learn.microsoft.com/en-us/dotnet/api/microsoft.office.interop.excel.workbooks.open?view=excel-pia#Microsoft_Office_Interop_Excel_Workbooks_Open_System_String_System_Object_System_Object_System_Object_System_Object_System_Object_System_Object_System_Object_System_Object_System_Object_System_Object_System_Object_System_Object_System_Object_System_Object_
        EXCEL.Workbook workbook = ExcelApp.Workbooks.Open(Filename: fdesc.FileInfo.FullName, ReadOnly: true, IgnoreReadOnlyRecommended: true, AddToMru: false);

        // save the doc
        /// https://learn.microsoft.com/en-us/dotnet/api/microsoft.office.interop.excel._workbook.saveas?view=excel-pia

        if (workbook.HasVBProject)
        {
            FileInfo newFile = new FileInfo(newname);
            newname = newFile.DirectoryName + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension(newFile.Name) + FileExt.NEW_Excel_Macro;
            UpateNewFileNameConsole(new FileInfo(newname));
            workbook.SaveAs(Filename: newname, FileFormat: EXCEL.XlFileFormat.xlOpenXMLWorkbookMacroEnabled, ReadOnlyRecommended: false, AddToMru: false);
        }
        else
        {
            workbook.SaveAs(Filename: newname, FileFormat: EXCEL.XlFileFormat.xlOpenXMLWorkbook, ReadOnlyRecommended: false, AddToMru: false);
        }


        // close the Workbook
        workbook.Close(SaveChanges: false);
    }
    else { throw new Exception("unkown File in conversion"); }

    //move the old file
    File.Move(fdesc.FileInfo.FullName, moveDir.FullName + Path.DirectorySeparatorChar + fdesc.FileInfo.Name);

}
catch (Exception ex)
{
    Debug.WriteLine(ex.Message);
}


//==============================================================================


public static class FileExt
{
    public const string OLD_Word = ".doc";
    public const string NEW_Word = ".docx";
    public const string OLD_Excel = ".xls";
    public const string NEW_Excel = ".xlsx";
    public const string NEW_Excel_Macro = ".xlsm";
    public const string RichTextFormat = ".rtf";
    public const string OldFormatDir = "!old_format";
    }
}

运行代码后 Windows 文件资源管理器的屏幕截图: enter image description here

最佳答案

这可能会因客户端操作系统版本而略有不同。

要清除 Windows 10 中的文件资源管理器历史记录,请手动打开注册表编辑器应用程序。 转到以下注册表项: HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer

删除名为 TypedPaths 的子项:

enter image description here

打开文件资源管理器,转到文件夹 %APPDATA%\Microsoft\Windows\Recent\ 并删除您看到的所有文件和文件夹。


虽然 history 文件夹是隐藏的,但您仍然可以获取您创建的文件并以编程方式删除它们,例如:

string[] recentFiles = System.IO.Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.History), "*", SearchOption.AllDirectories);

foreach (var file in recentFiles)
{
   System.IO.File.Delete(file);
}

使用代码删除注册表项:

string keyName = @"Software\Microsoft\Windows\CurrentVersion\Explorer";
using (RegistryKey key = Registry.CurrentUser.OpenSubKey(keyName, true))
{
    if (key == null)
    {
        // Key doesn't exist. Do whatever you want to handle this case
    }
    else
    {
        key.DeleteValue("TypedPaths");
    }
}

在尝试此操作之前备份您的注册表。

关于c# - 操作方法 : Stop adding files to "Recent files" in Quick Access when opening/saving files with Microsoft. Office.Interop,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56385111/

相关文章:

c# - Excel Interop 获取单元格内文本的像素宽度

c# - 在单个 foreach C# 中迭代​​ 2 个形状列表

c# - 数据迁移 MS SQL - C# 中的 MySQL

c# - Winform 与 EF Code First 和 Mysql

c# - 使用 C# Interop 清除/清空办公室剪贴板

c# - 记事本如何在最近打开的文件夹中创建快捷链接

c++ - 如何更新 CMFCRibbonBar 中的最近文件列表

c# - 表单中的嵌套异步和等待

c# - 通过 OleDb 的 Excel 以不同方式显示数字...取决于电子表格是否打开?