c# - 无法关闭和删除我检查是否已损坏的打开文件 (.docx)
问题描述
我遇到的问题是关于 .docx 文档的验证,以验证我正在使用下一个方法:
public static bool ValidateWordDocument(string filepath)
{
WordprocessingDocument wordprocessingDocument;
try
{
wordprocessingDocument = WordprocessingDocument.Open(filepath, true);
wordprocessingDocument.Close();
return true;
}
catch (Exception e)
{
_logger.LogError($"El archivo {filepath} esta corrupto o se trata de un archivo ");
return false;
}
}
但是当它启动异常时(因为文件已损坏并且无法打开它)使文件保持打开状态并且无法在 catch 中关闭,因为它脱离了 WordprocessingDocument“实例?”的上下文。
然后,当我必须删除必须验证的文件时,我不能,因为它已被另一个进程打开: 错误删除
谢谢你。
解决方案
也许将其更改为这样会有所帮助:
try
{
wordprocessingDocument = WordprocessingDocument.Open(filepath, true);
return true;
}
catch (Exception e)
{
_logger.LogError($"El archivo {filepath} esta corrupto o se trata de un archivo ");
return false;
}
finally{
wordprocessingDocument.Close();
}
推荐阅读
- c# - Xamarin.Mac 中的 NSPasteboard
- javascript - Vue.js:如何在动态 v-for 中进行验证
- .net - Nginx - 将 HTTP 重定向到 HTTPS
- javascript - 将新文档插入firebase时如何增加帖子ID?
- vb.net - 更改 DataGridView 中数字的形状
- excel - 使用字体名称填充组合框
- python-3.x - Python Gmail API:使用多个过滤器查询时没有返回结果
- c# - 当包含另一个可观察集合的可观察集合中的属性发生更改时,UWP C# UI 不会更新
- multithreading - 互斥体。甚至是什么?
- opencv - OpenCV - OpenGL - OpenCL 互操作