0

Im working on a Discord bot and want to create this:

I have a string that reads the content of a local file, but I want to upload that file to a specific website (for example GitHub) and use it, instead of the local file (basically, I want to get rid of the local file and be able to use the uploaded file).

I have this simple line of code:

string[] contentFile = File.ReadAllLines("files/file.txt");

Can somebody help me out here?

Edit: The file contains the lines "1", "2", "3" (and so on), and I want to put these lines in a string array so that I can get access to "1" with contentFile[0], to "2" with contentFile[1] and so on and so forth. It works perfect with a local file, but I do not know how to get access to the uploaded file and the lines within.

RetroS
  • 1
  • 1
  • Possible duplicate of [How to get content from file from this URL?](https://stackoverflow.com/questions/12240857/how-to-get-content-from-file-from-this-url) – RoboYak Jun 12 '17 at 21:00
  • Start reading the GitHub [documentation](https://developer.github.com/v3). – Jeroen Heier Jun 12 '17 at 21:01
  • @ElijahTate I want to get the content into a string array. – RetroS Jun 12 '17 at 21:06
  • @JeroenHeier GitHub was just an example. – RetroS Jun 12 '17 at 21:06
  • Does that help? https://stackoverflow.com/questions/7924160/parsing-a-url-into-an-array the only difference would be that your str[] would be separated by " " I am guessing. – RoboYak Jun 12 '17 at 21:14
  • Well, it's the uri-class, and I don't need that. – RetroS Jun 12 '17 at 21:34
  • `It works perfect with a local file, but I do not know how to get access to the uploaded file and the lines within.` Then you download the uploaded file and access it, then afterwards delete the downloaded file once you are done making the array. Have you tried that yet? – WQYeo Jun 13 '17 at 06:23
  • @WenQin I did, you can see the result in my anwer. – RetroS Jun 13 '17 at 14:15

1 Answers1

0

So this is how I got it to work (if anyone is interested):

            using (var client = new WebClient())
        {
            client.DownloadFile("www.website.com/file.txt", "files/file.txt");
        }

        string[] fileContent = File.ReadAllLines("files/file.txt");

        while (true)
        {
            try
            {
                if (File.Exists("files/file.txt")) File.Delete("files/file.txt");
                break;
            }
            catch (IOException)
            {
                codeTrue = true;
            }
        }

This basically downloads the file, reads it and deletes it afterwards. The only "problem" is that everytime I update the file on the website, I need to restart the application, else the string array wont be updated. You can solve that problem by adjusting the code a bit and creating an update-loop.

RetroS
  • 1
  • 1