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). 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.
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. 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. This happens in real-time as I step through the code and it causes the CPU usage spike from the explorer.exe process. 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. 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.
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.
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";
}
}
Screenshot of Windows File Explorer after running the code for a bit:


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. Go to the following Registry key:
HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\ExplorerDelete the subkey named TypedPaths:
Open File Explorer, go to the folder
%APPDATA%\Microsoft\Windows\Recent\and delete all files and folders you see.Although the history folder is hidden you can still fetch the files you created and delete them programmatically, eg:
To delete the registry key using code:
Make a backup of your registry before trying this.