如何检查文件夹或文件是否被锁定,打开或正在使用

时间:2018-07-25 02:09:53

标签: c# uwp

我在上下搜索,寻找一个好的解决方案,以检查UWP应用程序的文件夹或文件是否被锁定。我发现的大多数东西都使用某种流非常过时。经过长时间的搜索,我发现一个评价较低的帖子,表明如果内容在使用中,Windows将不允许重命名文件夹或文件。

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

因此,我发布了这些我发现的快速解决方案,希望它们能起作用,希望它可以通过无尽的搜索帮助其他人。

1 个答案:

答案 0 :(得分:0)

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