Winforms C# Read random line from Pastebin

MetaGota

New member
Joined
Sep 25, 2021
Messages
1
Programming Experience
1-3
Hello, I'm trying to find a way to read a random line from a Pastebin


C#:
            WebClient WC = new WebClient();
            List<string> TEST = new List<string>();

            Random Rand = new Random();

            StreamReader testReader = new StreamReader(WC.DownloadString("Pastebin"));

            string line = "";
            while (!testReader.EndOfStream)
            {
                line = testReader.ReadLine();
                TEST.Add(line);
            }
            button1.Text = TEST[Rand.Next(1, TEST.Count)];

I was doing this blindly off a tutorial, and I thought I could read off a WebLink but then I remembered it only supports files. I was thinking of putting the Pastebin text into a .txt document but I think it is sorta Inefficient. So just to summarize I want to read a random line off a Pastebin Link/WebLink

EDIT:
Ok I fixed the code

C#:
            string line = "";
            WebClient WC = new WebClient();
            List<string> TEST = new List<string>();

            Random Rand = new Random();

            line = WC.DownloadString("https://pastebin.com/raw/GfCFbBAA");
            TEST.Add(line);

            button1.Text = TEST[Rand.Next(1, TEST.Count)];
System.ArgumentOutOfRangeException: 'Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index'

It gives this error
System.ArgumentOutOfRangeException: 'Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index'
 
Last edited:
You are adding only a single item to your list. Lists are zero indexed. You are asking the random number generator to return a number whose minimum value of 1. 1 is out of range of the list.
 

Latest posts

Back
Top Bottom