Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get text from txt file on the internet

Tags:

c#

uwp

I have a uwp and i need to get text from a txt file saved on the internet to string I have a problem with download the file and get the text to string

Here's my code:

            var webRequest = WebRequest.Create(@"http://yourUrl");
        using (var response = webRequest.GetResponse())
        using (var content = response.GetResponseStream())
        using (var reader = new StreamReader(content))
        {
            var strContent = reader.ReadToEnd();
        }

can anyone help me?

like image 851
Louay Sleman Avatar asked Sep 14 '25 23:09

Louay Sleman


2 Answers

var url = "http://example.com/abc.txt";
var textFromFile = (new WebClient()).DownloadString(url);
like image 76
m-tech Avatar answered Sep 17 '25 14:09

m-tech


I find an answer to my question

I've to download the txt file to my local storage and after that i read them from my local

Here's the code that i used to download the file to my local folder

            var uriBing = new Uri(@"https://your/abc.txt");
        StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
        StorageFile sampleFile = await storageFolder.CreateFileAsync("status.txt", CreationCollisionOption.ReplaceExisting);
        var cli = new HttpClient();
        Byte[] bytes = await cli.GetByteArrayAsync(uriBing);
        IBuffer buffer = bytes.AsBuffer(); await Windows.Storage.FileIO.WriteBufferAsync(sampleFile, buffer);

Thanks for helping hope this will help anyone look for something like this

like image 20
Louay Sleman Avatar answered Sep 17 '25 13:09

Louay Sleman