locked
Access denied at saving file in uwp RRS feed

  • Question

  • User393891 posted

    I tried to save a file in uwp, but the access denied.

                    String fileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Record", "record.opml");
                    String dirName = Path.GetDirectoryName(fileName);
    
                    if (!Directory.Exists(dirName))
                        Directory.CreateDirectory(dirName);
    
                    if (!File.Exists(fileName))
                        File.Create(fileName);
    
                    using (var streamWriter = new StreamWriter(fileName, true))
                    {
                        streamWriter.WriteLine(doc);
                    }
    
    Monday, May 18, 2020 11:58 PM

Answers

  • User369979 posted

    We could only access the app's own data storage or app's install directory directly: https://docs.microsoft.com/en-us/windows/uwp/files/file-access-permissions If you want to read or write files outside the application we need to use broadFileSystemAccess or file open picker. Since Storage api belongs to UWP only we have to use dependency service to complete this. Firstly, create an interface on forms:

    public interface IFileManager
    {
        Task writeFileTo(byte[] data, string fileName);
    }
    

    And then implement it on UWP:

    [assembly: Dependency(typeof(FileManager))]
    namespace Sample.UWP
    {
        public class FileManager : IFileManager
        {
    
            public async Task writeFileTo(byte[] data, string fileName)
            {
                var folder = await StorageFolder.GetFolderFromPathAsync(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile));
                var recordFolder = await folder.CreateFolderAsync("Record", CreationCollisionOption.OpenIfExists);
                StorageFile sampleFile = await recordFolder.CreateFileAsync(fileName, CreationCollisionOption.OpenIfExists);
                await Windows.Storage.FileIO.WriteBytesAsync(sampleFile, data);
                return;
            }
        }
    }
    

    Finally, consume code:

    await DependencyService.Get<IFileManager>().writeFileTo(bytes, "record.opml");
    

    The user needs to check on the file access here:

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, May 19, 2020 3:03 AM

All replies

  • User369979 posted

    We could only access the app's own data storage or app's install directory directly: https://docs.microsoft.com/en-us/windows/uwp/files/file-access-permissions If you want to read or write files outside the application we need to use broadFileSystemAccess or file open picker. Since Storage api belongs to UWP only we have to use dependency service to complete this. Firstly, create an interface on forms:

    public interface IFileManager
    {
        Task writeFileTo(byte[] data, string fileName);
    }
    

    And then implement it on UWP:

    [assembly: Dependency(typeof(FileManager))]
    namespace Sample.UWP
    {
        public class FileManager : IFileManager
        {
    
            public async Task writeFileTo(byte[] data, string fileName)
            {
                var folder = await StorageFolder.GetFolderFromPathAsync(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile));
                var recordFolder = await folder.CreateFolderAsync("Record", CreationCollisionOption.OpenIfExists);
                StorageFile sampleFile = await recordFolder.CreateFileAsync(fileName, CreationCollisionOption.OpenIfExists);
                await Windows.Storage.FileIO.WriteBytesAsync(sampleFile, data);
                return;
            }
        }
    }
    

    Finally, consume code:

    await DependencyService.Get<IFileManager>().writeFileTo(bytes, "record.opml");
    

    The user needs to check on the file access here:

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, May 19, 2020 3:03 AM
  • User393891 posted

    Thanx @LandLu

    Tuesday, May 19, 2020 3:56 PM
  • User395363 posted

    How to put my app into "Choose which apps can access your file system" ?

    Wednesday, June 24, 2020 11:03 AM