locked
How can you get a file from a folder that is not directly under that folder

    Question

  • In my app, I have a folder that is chosen by the user. I need to programmatically open files under that folder that may be nested in subfolders. How can I achieve this? Can I open them from a relative path? I tried opening from an absolute path that passes through the folder I own, but I still get an access denied exception. 

    Thanks!

    David


    • Edited by davidb353 Tuesday, April 15, 2014 1:29 AM
    Monday, April 14, 2014 11:39 PM

Answers

  • Hi davidb353,

    Of course you can.

    I gained the access permission for my Desktop via FolderOpenPicker and I was able to access the file in the folder named "Team" which is a subfolder of my Desktop folder. I can access the file without any exception.

    See the code below:

                StorageFolder folder = await folderPicker.PickSingleFolderAsync();
                if (folder != null)
                {
                    StorageFolder subfolder = await folder.GetFolderAsync("Team");
                    if(subfolder != null)
                    {
                        StorageFile file = await subfolder.GetFileAsync("cpteam.txt");
                        if(file != null)
                        {
                              // do something here.
                        }
                    }
                }

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Marked as answer by davidb353 Tuesday, April 15, 2014 3:24 PM
    Tuesday, April 15, 2014 5:53 AM
    Moderator
  • StorageFolder.GetFileAsync takes a relative path.


    Visual C++ MVP

    • Marked as answer by davidb353 Tuesday, April 15, 2014 5:49 PM
    Tuesday, April 15, 2014 5:39 PM

All replies

  • Hi davidb353,

    Of course you can.

    I gained the access permission for my Desktop via FolderOpenPicker and I was able to access the file in the folder named "Team" which is a subfolder of my Desktop folder. I can access the file without any exception.

    See the code below:

                StorageFolder folder = await folderPicker.PickSingleFolderAsync();
                if (folder != null)
                {
                    StorageFolder subfolder = await folder.GetFolderAsync("Team");
                    if(subfolder != null)
                    {
                        StorageFile file = await subfolder.GetFileAsync("cpteam.txt");
                        if(file != null)
                        {
                              // do something here.
                        }
                    }
                }

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Marked as answer by davidb353 Tuesday, April 15, 2014 3:24 PM
    Tuesday, April 15, 2014 5:53 AM
    Moderator
  • Thanks James. But what if the file is more deeply nested under the folder you own? Do you have to split the path and get each folder one-by-one and finally get the file? I wish I could just open it with a relative path from the folder.

    Tuesday, April 15, 2014 3:26 PM
  • StorageFolder.GetFileAsync takes a relative path.


    Visual C++ MVP

    • Marked as answer by davidb353 Tuesday, April 15, 2014 5:49 PM
    Tuesday, April 15, 2014 5:39 PM
  • Really? I have tried multiple times. In my test case the relative path for the file is "Backgrounds/file.png" but passing this string doesn't work. What am I doing wrong?

    Tuesday, April 15, 2014 5:45 PM
  • Hah. I needed "Backgrounds\\file.png"

    Stupid mistake. Sorry, and thanks.

    Tuesday, April 15, 2014 5:49 PM