c# - 如何检查文件夹或文件是否被锁定、打开或正在使用
问题描述
我已经到处搜索了一个很好的解决方案来检查文件夹或文件是否为 UWP 应用程序锁定。我发现的大多数东西都使用某种流非常过时。经过长时间的搜索,我发现了一篇低评价的帖子,表明如果内容正在使用,Windows 将不允许重命名文件夹或文件。
我意识到处理这种情况的最佳方法是通过适当的异常使用,但是我有一个应用程序可以归档文件,但由于打开了文件,所以最终无法删除原始文件夹。这会产生各种无法处理的意外结果,包括原始用户数据丢失。
因此,我发布了这些我发现的快速解决方案,希望它可以从无休止的搜索中帮助其他人。
解决方案
/// <summary>
/// Check if StorageFile is locked. Return true if locked, false otherwise.
/// </summary>
/// <param name="StorageFileToCheck">StorageFile to check if locked.</param>
/// <returns></returns>
public static async Task<Boolean> StorageFileLocked(StorageFile StorageFileToCheck)
{
// If StorageFileToCheck can't be deleted, then rename attempt will cause System.UnauthorizedAccessException indicating StorageFileToCheck is locked.
string stringFilenameTemp = Guid.NewGuid().ToString("N") + StorageFileToCheck.FileType;
string stringFilenameOrig = StorageFileToCheck.Name;
Debug.WriteLine($"StorageFileLocked(): stringFoldernameTemp={stringFilenameTemp}");
Debug.WriteLine($"StorageFileLocked(): stringFoldernameOrig={stringFilenameOrig}");
try
{
// Try to rename file. If file is locked, then System.UnauthorizedAccessException will occur.
await StorageFileToCheck.RenameAsync(stringFilenameTemp);
await StorageFileToCheck.RenameAsync(stringFilenameOrig); // Restore original name if no excpetion.
Debug.WriteLine($"StorageFileLocked(): Returned false since no exception.");
return false; // Return false since no exception. File is not locked.
}
catch (Exception ex)
{
Debug.WriteLine($"StorageFileLocked(): Returned true since exception occurred. Type={ex.GetType()}");
return true; // Return true since exception. File is locked.
}
}
/// <summary>
/// Check if StorageFolder is locked. Return true if locked, false otherwise.
/// </summary>
/// <param name="StorageFolderToCheck">StorageFolder to check if locked.</param>
/// <returns></returns>
public static async Task<Boolean> StorageFolderLocked(StorageFolder StorageFolderToCheck)
{
// If StorageFolderToCheck can't be deleted, then rename attempt will cause System.UnauthorizedAccessException indicating StorageFolderToCheck is locked.
string stringFoldernameTemp = Guid.NewGuid().ToString("N");
string stringFoldernameOrig = StorageFolderToCheck.Name;
Debug.WriteLine($"StorageFolderLocked(): stringFoldernameTemp={stringFoldernameTemp}");
Debug.WriteLine($"StorageFolderLocked(): stringFoldernameOrig={stringFoldernameOrig}");
try
{
// Try to rename folder. If folder is locked, then System.UnauthorizedAccessException will occur.
await StorageFolderToCheck.RenameAsync(stringFoldernameTemp);
await StorageFolderToCheck.RenameAsync(stringFoldernameOrig); // Restore original name if no excpetion.
Debug.WriteLine($"StorageFolderLocked(): Returned false since no exception.");
return false; // Return false since no exception. Folder is not locked.
}
catch (Exception ex)
{
Debug.WriteLine($"StorageFolderLocked(): Returned true since exception occurred. Type={ex.GetType()}");
return true; // Return true since exception. Folder is locked.
}
}
推荐阅读
- microsoft-graph-api - 在 Azure Durable Function 中读取图形用户
- ios - ios中的障碍,如android ConstraintLayout
- android - 错误:A RenderFlex 底部溢出 99524 像素
- apache-spark - 使用 Spark 引擎执行 SQL 时如何在 hive UDF 中获取 Spark 的 partitionId 或 taskContext?
- prolog - Prolog:为什么输出根据他们拥有的孩子数量显示相同数量的输出
- reactjs - 如何在子组件发出 POST 请求后立即从父组件渲染?
- javascript - 有没有办法让 CSS 动画在重复点击按钮时再次运行而不使用超时?
- teradata - MLOAD 和 FastLoad 错误:外部加载程序进程退出并出现错误 [12] 和/或 [8]
- node.js - 如何在快速路由参数、猫鼬中按名称及其 id 获取数据
- c# - 在 Cosmos db 中添加文档数据会导致错误,因为资源已经存在