Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read an certain line from text file with UWP app?

Tags:

uwp

How do I read for example only the second line from text file containing of four lines located on the Windows Phone SD card or internal storage with UWP app coded in Visual C#?

Does internal storage mean the same as Computer\Windows phone\Phone in Explorer?

like image 623
Ken Avatar asked Nov 27 '25 08:11

Ken


1 Answers

Since there are not a lot of details in your question I will assume the file you need is deployed with the application.

public async Task<string> ReadLine(int lineIndex)
{
    var path = @"test.txt";
    var folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
    var file = await folder.GetFileAsync(path);
    var lines = await Windows.Storage.FileIO.ReadLinesAsync(file);
    if (lines.Count >= lineIndex + 1)
    {
        return line[lineIndex];
    }
    else
    {
       return null;
    }
}

If the file is not deployed with the app, change the folder property to another one from here: Windows.Storage.ApplicationData.Current. (LocalFolder, TempFolder, RoamingFolder)

like image 183
AlexDrenea Avatar answered Dec 02 '25 05:12

AlexDrenea



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!