2

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.

Noorul
  • 873
  • 2
  • 9
  • 26
  • Can you give the fileName in `fileName = fileName + file.FileType;` – lindexi May 31 '17 at 07:52
  • Can you give the folder path? – lindexi May 31 '17 at 07:52
  • Folder path "C:\Users\User1\AppData\Local\Packages\8ba23e35-0038-47d9-a34b-0aea5001fea7_vaa7z6zwjw9vp\LocalState\FileCardFiles" – Noorul May 31 '17 at 08:43
  • File name "1067da5e-af8d-42f6-9272-ad6bf9a0d53d.docx" , I am creating this file name by GUID , and am also checking if it is available already in the storage. – Noorul May 31 '17 at 08:44
  • I dont think it too long. – lindexi May 31 '17 at 11:03
  • Could you show more detail about `GetSubFolder` method? – Nico Zhu Jun 01 '17 at 08:36
  • @NicoZhu-MSFT Thanks for your reply , I don't think GetSubfolder method cause any issue, since it will just get the appropriate folder for a file, anyway i will add that method too. Please see the edited question. – Noorul Jun 05 '17 at 06:07
  • As you said the `GetSubfolder` method did not cause the issue. And I have tried to reproduce the issue with your code. But it works well in my project. – Nico Zhu Jun 05 '17 at 09:56
  • Is it? are you able to save .doc and .docx files too? without error. – Noorul Jun 06 '17 at 07:22
  • The sole problem is I am not able to create a file with ".doc" extension, like "somthing.doc" :( . – Noorul Jun 06 '17 at 10:51
  • Yes . I can save "somthing.doc" without error. – Nico Zhu Jun 07 '17 at 01:40
  • yeah, you are right, I tried my app in some other system , its working fine. And I can save it with .doc format. I am not sure which cause the problem in my laptop. – Noorul Jun 08 '17 at 05:18
  • Please try to uninstall your app and clean the solution rebuild it again. – Nico Zhu Jun 09 '17 at 05:47

0 Answers0