使用以下代码打开和保存 Word/Excel 文档时,打开和保存的文件将被添加到 Windows 文件资源管理器的最近文件中(见屏幕截图)。代码本身适用于我的目的;我只复制了一小部分与环绕相关的代码。但是,我在阻止这种不良行为方面遇到了困难,并且搜索互联网似乎只给了我关于如何防止文件出现在办公应用程序本身的“最近文件”列表中的结果,而不是 Windows 文件资源管理器。
我在包含旧的非 xml 格式的 Office 文件的上千个目录上运行此代码,有些甚至分成 5 位数。当.Open()
被调用时,原始文件显示在列表中,当.SaveAs()
/.SaveAs2()
被调用时,新文件显示在列表中。当我逐步执行代码时,这会实时发生,它会导致 explorer.exe 进程的 CPU 使用率飙升。打开和重新保存旧格式的 office 文件的操作发生得相当快,我怀疑这会由于 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://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";
}
}