sticky
File access on Windows IoT Core RRS feed

  • General discussion

  • In general, it is convenience for storing the data on local file system by default. But in many scenarios, it is necessary to store the data on remote shared folder or removable storage, so that we can share and protect the data. 

    Universal Windows Apps (apps) can access certain file system locations by default. Apps can also access additional locations through the file picker, or by declaring capabilities. File access permissions in UWP are according to locations and file types. In other words, even though you have authorized to access the location, you also need to add the FileTypeAssociation in the appmanifest. A file type association indicates that the app is registered to handle files of the specified types. You can follow a tutorial in this blog to learn how to launch your app by double-clicking an associated file (and your app can do something with that file of course).

    Following introduces how to access local, removable storage and remote files shared in network on windows iot core.

    1.Access local file system:

    When you create a new app, you can access Application install directory and Application data locations by default. Application install directory is the folder where your app is installed on the user's system. Application data locations include three folders(local, roaming and temporary), they are created when the app is installed.

    In addition, you can access certain files and folders by declaring capabilities in the app manifest

    , or by calling a file picker to let the user pick files and folders for the app to access.

    Please note that you can't access the local, roaming, or temporary folders through the file picker.

    More detail information please refer to this document.

    On Windows IoT Core, if you don’t want to use file picker to access the folders on your device, you can use FolderPermissions tool to make a folder to accessible for UWP app. For example run following command to give UWP apps permission to access to c:\test folder. Please note that FolderPermissions tool only supports on windows iot core.

    FolderPermissions c:\test -e

    *Please note that, FolderPermissions will work only with native Win32 apis for eg. CreateFile2 and not with WinRT apis like StorageFolder, StorageFile etc.

    2.Access removable storage:

    The removableStorage capability provides programmatic access to files on removable storage, like USB keys and external hard drives. But please note that the hardware needs to report to Windows as removable storage first. You can use following command to review the devices local drivers.

    gdr -PSProvider 'FileSystem'

    You will get a list of all drives attached (include system drive and the removable storage drive). And then you need to add the Removable Storage capability to the appxmanifest. Please refer to this sample:

    Windows.Storage.StorageFolder devices = Windows.Storage.KnownFolders.RemovableDevices;

    var folders = await devices.GetFoldersAsync();

    if(folders.Count > 0)

    {

                    Windows.Storage.StorageFile file = await folders[0].GetFileAsync("welcome3.jpg");

                    if (file != null)

                    {

                                    var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);

                                    var image = new BitmapImage();

                                    image.SetSource(stream);

                                    ImageSvgItem.Source = image;

                    }

    }

    In above code, it accesses an image file named welcome3.jpg which is located in an USB stick and displays the image in an UWP page.

    *Please note:

    • gdr command is a powershell command,if you want to run this command, you need to connect the device with powershell remotely, please refer to this document.
    • You must add File Type Associations to your app manifest that declare specific file types that your app can access in this location. But if you added the broadFileSystemAccess capability for the app, it would not be necessary, but this capability only supports on desktop currently.

    3. SQLite in UWP on windows iot core:

    If we want to store the data file in a removable storage or location except Application install directory and Application data locations, it is prohibited to write data(IOError) even though use FolderPermissions to authorize the access permissions, but the sqlite database file could be access as read-only. One alternative is to allow users to import/export their database file from/to arbitrary locations using a picker. Here is an open issue on GitHub about this problem.

    4. Access removable storage in Background App:

    Currently, Microsoft has not added removable storage access to background apps. You can create a foreground app and use app bridge to access removable storage in background. We can refer to this topic.


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    Monday, November 5, 2018 2:47 AM
    Moderator

All replies

  • Hi Michael,

    The link:https://github.com/ms-iot/ntvsiot/issues/62 in the topic which mentioned in the "4. Access removable storage in Background App:" is missing. Is there more information about the solution?

    Monday, April 1, 2019 8:28 AM
  • Hello moyanming,

    At the moment, background apps can not support File Type Association extension. It's known issue.

    If there is any update, i will let you know.

    Best Regards,

    Michael


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, April 8, 2019 7:13 AM
    Moderator