storageFolder.CreateFileAsync throws UnauthorizedAccessException

818 Views Asked by At

I am creating a UWP app, in which am storing file in local folders. Everything working fine untill i try saving a .doc file.

1) I am saving file with extensions (ex: filename.mp3 , filename.flv , filename.png).

2) I am not getting any exception while save this file.

3) but when I try to save .doc or .docx files like for example "filename.doc" I am getting "UnauthorizedAccessException" exception.

4)Important thing here is if I try saving the same file without extension in file name "filename" instead of "filename.doc" Its working fine, no exception raised.

5) I am getting exception only while saving with extension "filename.doc" , that is also only for ".doc" and ".docx" file types.

Here is the code

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));
        }

Here is GetSubFolder method

     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;
        }
    }

Can anyone help me to solve this. Thanks in advance, Noorul.

0

There are 0 best solutions below