storageFolder.CreateFileAsync抛出UnauthorizedAccessException

时间:2017-05-31 07:48:57

标签: uwp windows-store-apps .doc unauthorizedaccessexcepti storagefile

我正在创建一个UWP应用程序,其中将文件存储在本地文件夹中。 一切正常,直到我尝试保存.doc文件。

1)我正在保存带扩展名的文件(例如:filename.mp3,filename.flv,filename.png)。

2)保存此文件时,我没有收到任何异常。

3)但是当我尝试保存.doc或.docx文件时,比如" filename.doc"我得到了#34; UnauthorizedAccessException"异常。

4)重要的是,如果我尝试在文件名中保存没有扩展名的同一文件" filename"而不是" filename.doc"工作正常,没有例外。

5)我只有在使用扩展程序保存时才会出现异常" filename.doc" ,这也仅适用于" .doc"和" .docx"文件类型。

这是代码

StorageFolder fileFolder = await HelperFunctions.GetSubFolder(4);
        fileName = fileName + file.FileType;
        StorageFile storageFile = await fileFolder.CreateFileAsync(fileName);
        IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.Read);

        using (IRandomAccessStream tempStream = await storageFile.OpenAsync(FileAccessMode.ReadWrite))
        {
            await RandomAccessStream.CopyAndCloseAsync(fileStream.GetInputStreamAt(0), tempStream.GetOutputStreamAt(0));
        }

这是GetSubFolder方法

     public static async Task<StorageFolder> GetSubFolder(int type)
        {
            StorageFolder mainFolder = ApplicationData.Current.LocalFolder;
            Storage

            Folder subFolder;

        if (type == 0)
        {
            var result = await mainFolder.TryGetItemAsync(Constants.TextNotesFilesFolderName);

            //creating seperate folder for text files if not exist
            if (result == null)
                subFolder = await mainFolder.CreateFolderAsync(Constants.TextNotesFilesFolderName);
            else
                subFolder = await mainFolder.GetFolderAsync(Constants.TextNotesFilesFolderName);

            return subFolder;
        }
        else if (type == 1)
        {
            var result = await mainFolder.TryGetItemAsync(Constants.AudioFilesFolderName);

            //creating seperate folder for audio files if not exist
            if (result == null)
                subFolder = await mainFolder.CreateFolderAsync(Constants.AudioFilesFolderName);
            else
                subFolder = await mainFolder.GetFolderAsync(Constants.AudioFilesFolderName);

            return subFolder;
        }
        else if (type == 2)
        {
            var result = await mainFolder.TryGetItemAsync(Constants.ImageFilesFolderName);

            //creating seperate folder for image files if not exist
            if (result == null)
                subFolder = await mainFolder.CreateFolderAsync(Constants.ImageFilesFolderName);
            else
                subFolder = await mainFolder.GetFolderAsync(Constants.ImageFilesFolderName);

            return subFolder;
        }
        else if (type == 3)
        {
            var result = await mainFolder.TryGetItemAsync(Constants.ChecklistFilesFolderName);

            //creating seperate folder for checklist files if not exist
            if (result == null)
                subFolder = await mainFolder.CreateFolderAsync(Constants.ChecklistFilesFolderName);
            else
                subFolder = await mainFolder.GetFolderAsync(Constants.ChecklistFilesFolderName);

            return subFolder;
        }

        else
        {
            var result = await mainFolder.TryGetItemAsync(Constants.FileCardsFolderName);

            //creating seperate folder for text files if not exist
            if (result == null)
                subFolder = await mainFolder.CreateFolderAsync(Constants.FileCardsFolderName);
            else
                subFolder = await mainFolder.GetFolderAsync(Constants.FileCardsFolderName);

            return subFolder;
        }
    }

任何人都可以帮我解决这个问题。 提前致谢, Noorul。

0 个答案:

没有答案