简体   繁体   English

如何:使用Microsoft.Office.Interop打开/保存文件时,停止将文件添加到“快速访问”中的“最近的文件”中

[英]How-To: Stop adding files to “Recent files” in Quick Access when opening/saving files with Microsoft.Office.Interop

When opening and saving Word/Excel documents using the following code, the files that are opened and saved are being added to the Recent Files of Windows File Explorer (see screenshot). 使用以下代码打开和保存Word / Excel文档时,已打开和保存的文件将添加到Windows File Explorer的“最近文件”中(请参见屏幕截图)。 The code itself works fine for my purposes; 该代码本身可以很好地达到我的目的。 I've only copied a small portion with surround relevant code. 我只复制了与环绕声相关的代码的一小部分。 However, I am having difficulties in stopping this undesired behavior and searching the internet only seems to give me results on how to keep the files from showing up in the "Recent Files" list of the office applications themselves, not Windows File Explorer. 但是,我在阻止这种不良行为方面遇到困难,并且在Internet上搜索似乎只能为我提供有关如何防止文件显示在Office应用程序本身(而不是Windows File Explorer)的“最近文件”列表中的结果。

I am running this code on directories that contain in the upper thousands, some even break into 5 digit counts, of Office files in the old non-xml format. 我正在包含旧的非XML格式的Office文件的上千个目录中运行此代码,其中有些甚至可以分成5位数字。 When .Open() is called, the original file shows up in the list and when .SaveAs() / .SaveAs2() is called the new file shows up in the list. 调用.Open() ,原始文件显示在列表中;调用.SaveAs() / .SaveAs2() ,新文件显示在列表中。 This happens in real-time as I step through the code and it causes the CPU usage spike from the explorer.exe process. 当我单步执行代码时,这是实时发生的,并导致explorer.exe进程中的CPU使用率激增。 The act of opening and re-saving the old format office files happens rather quickly, and I suspect this causes a large CPU usage load due to explorer.exe constant processing the recent files. 打开并重新保存旧格式的Office文件的动作发生得很快,并且我怀疑由于explorer.exe不断处理最新文件而导致大量的CPU使用负载。 Other symptoms that I think are related is that the cursor constantly has the spin-wheel under it when the code is running and the whole OS GUI seems to become somewhat unresponsive. 我认为相关的其他症状是,在代码运行时,光标始终在其下旋转,而整个OS GUI似乎变得没有响应。

To be clear, I believe that a proactive solution which keeps Windows from adding files to the list will be the best path to take, rather than a retroactive solution which only does cleanup of the list after the fact. 明确地说,我认为防止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://docs.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://docs.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";
    }
}

Screenshot of Windows File Explorer after running the code for a bit: 在运行了一段时间的代码后,Windows File Explorer的屏幕截图: 在此处输入图片说明

This may vary slightly depending on the client Operating System version. 根据客户端操作系统的版本,这可能会略有不同。

To clear File Explorer history in Windows 10 manually open the Registry Editor app. 要在Windows 10中清除文件资源管理器历史记录,请手动打开注册表编辑器应用程序。 Go to the following Registry key: HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer 转到下面的注册表项: HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer

Delete the subkey named TypedPaths: 删除名为TypedPaths的子项:

在此处输入图片说明

Open File Explorer, go to the folder %APPDATA%\\Microsoft\\Windows\\Recent\\ and delete all files and folders you see. 打开文件资源管理器,转到文件夹%APPDATA%\\Microsoft\\Windows\\Recent\\然后删除看到的所有文件和文件夹。


Although the history folder is hidden you can still fetch the files you created and delete them programmatically, eg: 尽管历史记录文件夹是隐藏的,但是您仍然可以获取创建的文件并以编程方式将其删除,例如:

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

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

To delete the registry key using code: 要使用代码删除注册表项:

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");
    }
}

Make a backup of your registry before trying this. 尝试此操作之前,请备份注册表。

I am facing poor run-time performance due to CPU spikes from explorer.exe when the file entries are added to the Recent Files list. 当文件条目添加到“最近的文件”列表中时,由于来自explorer.exe的CPU峰值,我面临运行时性能下降的问题。

The act of opening and re-saving the old format office files happens rather quickly, and I suspect this causes a large CPU usage load due to explorer.exe constant processing the recent files. 打开并重新保存旧格式的Office文件的动作发生得很快,并且我怀疑由于explorer.exe不断处理最新文件而导致大量的CPU使用负载。 Other symptoms that I think are related is that the cursor constantly has the spin-wheel under it when the code is running and the whole OS GUI seems to become somewhat unresponsive. 我认为相关的其他症状是,在代码运行时,光标始终在其下旋转,而整个OS GUI似乎变得没有响应。

Can you first check these common causes - close background processes, in Device Mgr check the IO driver is OK and temporarily disable anti-virus: 您能否首先检查以下常见原因-关闭后台进程,在Device Mgr中检查IO驱动程序是否正常并暂时禁用防病毒功能:

  • Too many background processes 后台流程过多
  • Driver 司机
  • Anti-virus 反病毒
  • Malware 恶意软件
  • WmiPrvSE.exe WmiPrvSE.exe

  1. Downloaded Microsoft's Process Explorer to see what's causing the CPU spikes. 下载了Microsoft的Process Explorer,以查看导致CPU峰值的原因。 See here the high CPU consuming threads, it's this Audioses.DLL+0x1141b0. 请参阅此处的高CPU消耗线程,它是Audioses.DLL + 0x1141b0。 Can you show/tell me what DLL is taking up the highest CPU when you perform the operation? 您可以告诉/告诉我执行该操作时哪个DLL占用了最高的CPU吗? If its Explorer.EXE not Audioses.DLL consuming the CPU goto step 2 for your solution. 如果其Explorer.EXE不是Audioses.DLL,则消耗CPU转到解决方案的步骤2。 If it's something else we may need a PerfCounter trace. 如果还有其他问题,我们可能需要PerfCounter跟踪。

    在此处输入图片说明

  2. Run System File Checker (SFC) scan on the computer to scan for corrupt system files and replace them: http://support.microsoft.com/kb/929833 在计算机上运行系统文件检查器(SFC)扫描以扫描损坏的系统文件并替换它们: http : //support.microsoft.com/kb/929833

    "I can't believe that a broken icon would cause explorer.exe to go into a loop, eating up all of the CPU it can." “我不敢相信损坏的图标会导致explorer.exe陷入循环,从而耗尽所有可能的CPU。”

    and .. 和..

    "I had an exe with an corrupted icon on my desktop, moving it into a folder solved the problem" “我的台式机上的exe文件带有损坏的图标,将其移动到文件夹中即可解决问题”

  3. If the issue persists create a new user account, login as that user and check if the problem goes away, then you know your profile is corrupt. 如果问题仍然存在,请创建一个新的用户帐户,以该用户身份登录并检查问题是否消失,然后知道您的个人资料已损坏。

  4. Throw better hardware at the problem: Hardware is Cheap, Programmers are Expensive 向问题扔出更好的硬件: 硬件便宜,程序员昂贵

Ref's: 参考的:
https://www.techspot.com/community/topics/explorer-exe-high-cpu-usage-probably-not-malware.240788/ https://www.techspot.com/community/topics/explorer-exe-high-cpu-usage-probably-not-malware.240788/
https://answers.microsoft.com/en-us/windows/forum/all/explorerexe-high-cpu-usage-tried-everything-i-can/28cf8431-f9db-4169-9237-8e6521ef4c1c https://answers.microsoft.com/en-us/windows/forum/all/explorerexe-high-cpu-usage-tried-everything-i-can/28cf8431-f9db-4169-9237-8e6521ef4c1c
https://linustechtips.com/main/topic/939842-solvedexplorerexe0xa8150-high-cpu-usage/ https://linustechtips.com/main/topic/939842-solvedexplorerexe0xa8150-high-cpu-usage/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM