using readAllBytes throws "file not accessible error"

tim 160 Reputation points
2024-09-11T21:45:09.99+00:00

I trying to read an encrypted file using the following: and getting access not allowed error.

          // would prefer to use the following instead of picker"

           // AuthFile = @"C:\vbscripts\pid.txt";   

             // if exist.....

            var AuthFile = await FilePicker.PickAsync();   

            if (AuthFile != null)

            {                    

                try

                {                        

                    byte[] readText = File.ReadAllBytes(AuthFile.FullPath);

                    foreach (byte s in readText)

                    {

--------------- the following allowed access to "pid.txt" but was was not returning the Char only Diamonds with "?"'s

            if (AuthFile != null)

                                            {

                                                using (var stream = await AuthFile.OpenReadAsync())

                                                {

                                                    using (var reader = new StreamReader(stream))                                

                                                    {

                                                        // var AllContentFromFile = await reader.ReadToEndAsync(); 

                                                        while ((line = reader.ReadLine()) != null)

TIA

Tim

Universal Windows Platform (UWP)
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,858 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Junjie Zhu - MSFT 17,396 Reputation points Microsoft Vendor
    2024-09-12T03:04:11.54+00:00

    Hello @tim ,

    Welcome to Microsoft Q&A!

    trying to read an encrypted file using the following: and getting access not allowed error.

    UWP runs in a sandbox, by default UWP cannot access files in this location(C:\vbscripts\pid.txt). If you want to continue to access files directly through the path, you need to declare restricted capabilities for this, and open file permission in Settings. Or use the Windows.Storage.FileIO.ReadBufferAsync.

    var picker = new Windows.Storage.Pickers.FileOpenPicker();
    picker.ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail;        
    picker.FileTypeFilter.Add(".txt");
          
    Windows.Storage.StorageFile file = await picker.PickSingleFileAsync();
    if (file != null)
    {
                    
        var buffer = await Windows.Storage.FileIO.ReadBufferAsync(file);
        byte[] readText = buffer.ToArray();
        foreach (byte s in readText)
        {
    
        }
    }
    

    the following allowed access to "pid.txt" but was was not returning the Char only Diamonds with "?"'s

    This problem may be related to StreamReader reading. It is recommended that you refer to the sample reading file in the official documentation.

    Thank you.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.