首页 > 解决方案 > 如何检查文件夹或文件是否被锁定、打开或正在使用

问题描述

我已经到处搜索了一个很好的解决方案来检查文件夹或文件是否为 UWP 应用程序锁定。我发现的大多数东西都使用某种流非常过时。经过长时间的搜索,我发现了一篇低评价的帖子,表明如果内容正在使用,Windows 将不允许重命名文件夹或文件。

我意识到处理这种情况的最佳方法是通过适当的异常使用,但是我有一个应用程序可以归档文件,但由于打开了文件,所以最终无法删除原始文件夹。这会产生各种无法处理的意外结果,包括原始用户数据丢失。

因此,我发布了这些我发现的快速解决方案,希望它可以从无休止的搜索中帮助其他人。

标签: c#uwp

解决方案


/// <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.
        }
    }

推荐阅读