How to save a file in MyDocuments

Hi,
I'm making an app that need to create some csv files on the computer. I want thoses files to be saved in the user's Documents folder as the user need to be able to find them. But I couldn't find any mean to have access to the Documents folder. I've tried to add the documentsLibrary capability in the Package.appxmanifest but it doesn't let me create the files.
Is there any way to save files in such folder ?
Thank you for any help.

Best Answer

  • JulienTorrentJulienTorrent CH ✭✭
    Accepted Answer

    Tanks for the help.
    I had permissions problems with UWP and I made a FolderPicker and saved the location. I made it like that because otherwise it would give me an access denied error.

        FolderPicker folderPicker = new FolderPicker();
    
            folderPicker.SuggestedStartLocation = PickerLocationId.Desktop;
    
            folderPicker.FileTypeFilter.Add("*");
    
            StorageFolder folder = await folderPicker.PickSingleFolderAsync();
    
            StorageApplicationPermissions.FutureAccessList.Add(folder);
    

    And then I just need to open the saved location from a string.

              StorageFolder folder = await StorageFolder.GetFolderFromPathAsync(savedpath);
    
        var file = await folder.CreateFileAsync("test.csv");
    

Answers

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    I think you can use System.IO API with dependencyservices

  • seanydaseanyda GBMember ✭✭✭✭✭
    edited October 2017

    Use the DependencyService to access methods in the platform specific projects, which allow you to access folders on the phone.

    public string DocumentPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

    public void WriteLocalFile(string FileName, byte[] Data)
            {
                string filePath = Path.Combine(DocumentPath, FileName);
                File.WriteAllBytes(filePath, Data);
            }
    
  • JulienTorrentJulienTorrent CHMember ✭✭
    edited October 2017

    Hi,
    In my platform specifc code for uwp, I can't find Environment.GetFolderPath ( Environnent does not contain a definition for GetFolderPath )

    Do I need to add an using or something ?

  • seanydaseanyda GBMember ✭✭✭✭✭
    edited October 2017

    @JulienTorrent said:
    Hi,
    In my platform specifc code for uwp, I can't find Environment.GetFolderPath ( Environnent does not contain a definition for GetFolderPath )

    Do I need to add an using or something ?

    It uses the System using. But I have these on the same page.

    using System;
    using Xamarin.Forms;
    using UIKit;
    using System.IO;
    

    You should be able to right click on the error and it will tell you which libraries are required.

  • JulienTorrentJulienTorrent CHMember ✭✭
    Accepted Answer

    Tanks for the help.
    I had permissions problems with UWP and I made a FolderPicker and saved the location. I made it like that because otherwise it would give me an access denied error.

        FolderPicker folderPicker = new FolderPicker();
    
            folderPicker.SuggestedStartLocation = PickerLocationId.Desktop;
    
            folderPicker.FileTypeFilter.Add("*");
    
            StorageFolder folder = await folderPicker.PickSingleFolderAsync();
    
            StorageApplicationPermissions.FutureAccessList.Add(folder);
    

    And then I just need to open the saved location from a string.

              StorageFolder folder = await StorageFolder.GetFolderFromPathAsync(savedpath);
    
        var file = await folder.CreateFileAsync("test.csv");
    
  • Surajit_BeraSurajit_Bera Member ✭✭
    @JulienTorrent where is StorageFolder ?
      Is you solved it Android?
    
Sign In or Register to comment.