Akimiya
Akimiya

Reputation: 194

save Website Content & access it

I'm starting with C# again after 3 years (have average experience with object orientated languages; here I'm mainly missing function names). I'm not too sure it's possible in c#, so if you can recommend another language I will try to look there.

My Question(s):

  1. On program start (or button) I want to extract a part of a Website and save it (temporary of file don't matter). That way I wont need to buffer/load (loadtime) anything again and can access the content if I go offline afterward.
  2. I want to extract some numbers out of the content and do simple math with them.

Would be great to know if its possible and how. I'm happy if you can tell me the main functions I should look into. Some basic code would be great too if its not too much to ask.

Upvotes: 1

Views: 904

Answers (2)

coolmine
coolmine

Reputation: 4463

If you want to have access to the information even if your program closes/restarts then you will need to export the source code to a file as follows:

using (WebClient wb = new WebClient())
{
    string source = wb.DownloadString("http://example.com");

    File.WriteAllText("c:\\exampleFile.txt", source);
}

Otherwise you can remove the File.WriteAllText("c:\\exampleFile.txt", source); and simply parse the parts you want from the source and do your calculations.

Keep in mind this will download the source code of the url as 'it is' that means you will need to do some parsing of the text in order to get the information you want out of it.

Upvotes: 1

Shaharyar
Shaharyar

Reputation: 12449

May be you are looking for this:

var contents = new System.Net.WebClient().DownloadString(url);

Upvotes: 1

Related Questions