Driver Downloader


This is a little program I wrote in the first months of my apprenticeship.
We have a department for installing all computers on the university, and those computers need their drivers to work propeerly. Because some computers don’t want to work as we want them to, we have to search for the right drivers. This can be quiet frustrating and so we found a website called Driveridentifier . The only bad thing about this site is that we have to look in the source code of the website each time we want to download a driver because we want them without advertisment and other stuff. Alright so far so good, but why do it manually everytime, when you can write a programm which can search the source code for you and then download it?

Here we go.

screen2

 

 

 

 

 

 

This is what my form looks like, its pretty simple.
we paste a link in the textbox and then hit download, but what excactly happens when I hit the download button? Let’s take a look at it.

first we declare the variables we need for our project.
 public string url;
        public string filename;
        WebClient client = new WebClient();

So we need to get the url we want to take a look at and a WebClient for handling all http stuff. The filename string is for getting a filename to save it on you pc. Now let’s continue to our actual program.

        private void bt_download_Click(object sender, EventArgs e)
{

string strSource = client.DownloadString(textBox1.Text);
url = getBetween(strSource, "<a href=\"/scan/download_file.php?url=", "&hardware_id");
url = url.Replace("%2F", "/");
url = url.Replace("%3A", ":");
filename = url.Substring(url.LastIndexOf('/'));
filename = filename.Substring(1);

rtb_status.Text = "Datei wird von: " + url + " heruntergeladen";
rtb_status.Text += "\n Datei unter dem Namen: " + filename + " abgespeichert";
rtb_status.Text += "\n------------------------------\n";

Thread downloadfile = new Thread(download);
downloadfile.Start();

}

lets go through this peace of code. We get our url and pass it to our WebClient so it can download the source code of the website. After this is done we call our function getBetween ( I will explain it in one second, hold on ) and pass it the sourcecode with the parameters what to look at. After looking I found out a direct link to the manufactor is hidden in there, but its between <a href="/scan/download_file.php?url= and &hardware_id.

so let us just grab the code between those two and we are good to go. Oh no we have to replace some escape sequences because the sourcecode is in a different format.
After the replaced the symbols with the needed we can say that we want to cut the link into another piece, just to get our filename we want to save it under. We don’t want to save the file with an URL as filename, do we?

Because we don’t want the programm to get stuck or unaccesable while downloading big files we start a different thread for it. Now let’s look at our getBetween function.

       public static string getBetween(string strSource, string strStart, string strEnd)
{
int start;
int end;
if (strSource.Contains(strStart) && strSource.Contains(strEnd))
{
start = strSource.IndexOf(strStart, 0) + strStart.Length;
end = strSource.IndexOf(strEnd, start);
return strSource.Substring(start, end - start);
}
else
{
return "";
}
}

We see why we need the three parameters we give to the function now.
We look at the source code and search for the parts where the start and end are. When we found both parts we count the length from the start to end. After we got the length of these we make a substring out of it with the part between start and end. Now we got our link to download. Neat, we can download it now.

        public void download()
{
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);

client.DownloadFile(url, filename);

}

private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
}

Our download function first opens a new Handler for our Progressbar to show the status of the download. After this is done we take our WebClient again and tell it to download the file of the link we give it to and then what name it should be saved under on the pc.
Now that I look at it, it would have been nice to give a option to tell the programm where to save it under. Now we just download it in the current directory our programm runs in. That’s a thing you could try if you want to.

Our ProgressChanged void handles the status of the download and displays it as a ProgressBar.

It was nice too look at a old peace of code and go through it again. I can see where I already improved and what I would do different now. I hope you enjoyed it and stay tuned.

Leave a comment

Your email address will not be published. Required fields are marked *