Reading Selected file data in xamarin forms.UWP giving Access denied to path Exception

Sk 61 Reputation points
2024-04-04T09:36:38.24+00:00

I am trying to read data from selected file using below code. By using below able to read data from file in android and iOS but in UWP giving access denied exception.

Tried below link but didn't worked

https://www.jasongaylord.com/blog/2021/11/17/uwp-file-access-denied

var file = await FilePicker.PickAsync();
	if (file != null)
    {
        IList<string> currLine =File.ReadAllLines(file.FullPath);
        var count= currLine.Count;
    }
Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,348 questions
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,913 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 75,351 Reputation points Microsoft Vendor
    2024-04-05T02:29:07.3+00:00

    Hello,

    You can open file by file.OpenReadAsync and read it by StreamReader .

    I notice you want to read one line at a time and put all lines to a List. I use the following code to do it. You do not need update Application Capabilities

     var file = await FilePicker.PickAsync();
    int counter = 0; string line;
    IList<string> currLines = new List<string>();
    if (file != null)
    {
        using (var stream = await file.OpenReadAsync())
    
        {
    
            using (var reader = new StreamReader(stream))
    
            {
    
                // var AllContentFromFile = await reader.ReadToEndAsync();                       
    
                while ((line = reader.ReadLine()) != null)
    
                {
    
                    System.Console.WriteLine(line);
    
                    currLines.Add(line);
    
                    counter++;
    
                }
    
              
    
            }
    
          
    
        }           
    }
    var AllLines= counter;
    

    Best Regards,

    Leon Lu


    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.

    0 comments No comments

0 additional answers

Sort by: Most helpful

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.