0

I am trying to read the settings file from Edge browser's native host which is implemented in UWP. This settings file has some list of URLs to process it in extension. But I am not able to read the file in the native host. I am getting the following exception while reading the file.

System.UnauthorizedAccessException: 'Access to the path 'C:\Users\abc\Desktop\Settings.dat' is denied.'

string fileName = "C:\\Users\\Sameer\\Desktop\\Settings.dat"; 
bool bfileExist = File.Exists(fileName); 
if (bfileExist) { byte[] fileData = File.ReadAllBytes(fileName); }

does anyone have any idea to read the file in Edge browser's native host? Please let me know.

sam
  • 481
  • 2
  • 8
  • 21

2 Answers2

1

UWP has provided broadFileSystemAccess capability to access broader file with APIs in the Windows.Storage namespace. You need add the restricted broadFileSystemAccess capability before access. And please avoid use System.IO api. For more please refer the following code.

var file= await Windows.Storage.StorageFile.GetFileFromPathAsync(fileName);
if (file!= null)
{
    byte[] result;
    using (Stream stream = await file.OpenStreamForReadAsync())
    {
        using (var memoryStream = new MemoryStream())
        {

            stream.CopyTo(memoryStream);
            result = memoryStream.ToArray();
        }
    }

}
Nico Zhu
  • 32,367
  • 2
  • 15
  • 36
1

I have solved this by using following code

string fileName = "C:\\Users\\Sameer\\Desktop\\Settings.dat";
Windows.Storage.StorageFile file = await Windows.Storage.StorageFile.GetFileFromPathAsync(fileName);

if(file != null)
{
  var bufferData = await Windows.Storage.FileIO.ReadBufferAsync(file);               
  Windows.Storage.Streams.DataReader dataReader = Windows.Storage.Streams.DataReader.FromBuffer(bufferData);
  byte[] fileData = new byte[bufferData.Length];
  dataReader.ReadBytes(fileData);
}
sam
  • 481
  • 2
  • 8
  • 21