Title: Download and display a text file whenever a program starts in C#
When this example starts, it downloads a file from the internet and displays it. You could do something similar to display a message of the day or announcements for your program.
The program uses the following Load event handler to start the process.
// Download and display the text file.
private void Form1_Load(object sender, EventArgs e)
{
const string url = "https://raw.github.com/cubiclesoft/email_sms_mms_gateways/master/sms_mms_gateways.txt";
txtFile.Text = GetTextFile(url);
txtFile.Select(0, 0);
}
The code defines the URL of the file to download. It then calls the GetTextFile method described next to download the file and displays the result in the txtFile TextBox.
The following code shows the GetTextFile method.
using System.IO;
using System.Net;
...
// Get the text file at a given URL.
private string GetTextFile(string url)
{
try
{
url = url.Trim();
if (!url.ToLower().StartsWith("http")) url = "http://" + url;
WebClient web_client = new WebClient();
MemoryStream image_stream =
new MemoryStream(web_client.DownloadData(url));
StreamReader reader = new StreamReader(image_stream);
string result = reader.ReadToEnd();
reader.Close();
return result;
}
catch (Exception ex)
{
MessageBox.Show("Error downloading file " +
url + '\n' + ex.Message,
"Download Error",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
return "";
}
The code first uses the string class's Trim method to remove leading and trailing whitespace from the URL (just in case). Then if the URL doesn't begin with http, the code adds http:// to the beginning of the URL. (The code only checks for http so it can also handle https as in the URL used by this example.)
Next, the code creates a WebClient object. It uses the client's DownloadData method to get the file. DownloadData returns an array of bytes, so the program creates a memory stream associated with the array and then makes a StreamReader to read the memory stream. The code calls the reader's ReadToEnd method and closes the reader. Finally, it returns the string read by the StreamReader.
There are several ways this method could fail (if the file doesn't exist, it has improper permissions, the program fails to connect to the network, there's a network failure, and so forth), so the code does all of its work within a try-catch block.
With a bit more work, you could download other files such as pictures or RTF format files to display more than just text. If you really want a fancy announcement page downloaded from the internet, you might be better off placing a WebBrowser control on the form and making it navigate to a web page. Then you wouldn't even need to download the file explicitly. The WebBrowser would simply display the latest version of the web page.
If you examine the file that this example downloads, you'll see that it contains JSON-formatted data listing SMS gateway email addresses. My next post will provide a brief introduction to JSON. The post after that will explain how to read the data in the text file that this example downloads and displays.
Download the example to experiment with it and to see additional details.
|