Click here to Skip to main content
15,896,111 members
Articles / Desktop Programming / Windows Forms
Article

Programmatically detecting browser cache size for Firefox using C#

Rate me:
Please Sign up or sign in to vote.
3.13/5 (7 votes)
10 Jul 2008CDDL3 min read 22.7K   7  
Finding the Firefox browser cache size programmatically, using C#.

Introduction

This very small article is a walkthrough of the code I wrote to determine the browser cache size for Firefox installed on a particular PC.

Background

It was only recently, when I got stuck in my project, wherein I had to programmatically detect the Firefox browser cache size and determine whether the setting was optimal for the system's overall performance.

Using the code

In the code block that follows, you will notice that I have used a Registry key to determine whether Firefox is installed on the system or not.

For this, I used the SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall Registry key.

The following using directives must be used along with other required using directives:

C#
using Microsoft.Win32;
using System.IO;

All the applications that are installed on a system have a Registry entry within the aforementioned path within "HKEY_LOCAL_MACHINE".

Open the Registry key "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall" using the following code:

C#
string initPath = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall";
RegistryKey theKeyHKLMFFE = Registry.LocalMachine.OpenSubKey(initPath);

The folders within a particular key are known as subkeys, whereas, all the values within a particular key are known as value names.

To get the list of all the installed software, declare an array and use the "GetSubKeyNames()" method of the Registry key "theKeyHKLMFFE" to store the list of installed applications in the declared array.

Now, as we have the list of applications installed, we can find out if Firefox is installed or not. For this, run a loop through the array and for each array value, append it to the path mentioned above. This will give you the final path, from where you can fetch the display name of the software.

Now, open the Registry key for this path using the following lines of code:

C#
foreach (string soft in pathVals)
{
    finalPathName = initPath + "\\" + soft;
    finalPath = Registry.LocalMachine.OpenSubKey(finalPathName);

Next, store all the value names for the current application path in the array as follows:

C#
string[] subValueNames = finalPath.GetValueNames();

Now, you can iterate through this array to find if the display name contains Firefox. If any iteration returns true, store the install location in the variable InstallLocation. Also, set the boolean variable FirfoxExists to true. The complete code for the detection is as follows:

C#
string initPath = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall";
RegistryKey theKeyHKLMFFE = Registry.LocalMachine.OpenSubKey(initPath);
string[] pathVals = theKeyHKLMFFE.GetSubKeyNames();
theKeyHKLMFFE.Close();
FirfoxExists = false;
foreach (string soft in pathVals)
{
    finalPathName = initPath + "\\" + soft;
    finalPath = Registry.LocalMachine.OpenSubKey(finalPathName);
    string[] subValueNames = finalPath.GetValueNames();
    foreach (string subVals in subValueNames)
    {
        if (subVals == "DisplayName")
        { 
            if (finalPath.GetValue("DisplayName") != null && 
                finalPath.GetValue("DisplayName").ToString().
                                     ToUpper().Trim().Contains("FIREFOX"))
            {
                InstallLocation = finalPath.GetValue("InstallLocation").ToString();
                FirfoxExists = true;
                break;
            }
        }
    }
}

The above lines of code will check if Mozilla Firefox is installed on the system on which this code is running. If yes, it will set the boolean variable FirfoxExists to true and will store the install location of Firefox in the string variable InstallLocation.

Once we have figured out that Firefox is installed, the only work remaining is to open the all.js file, which resides in the greprefs folder within the current install location. The following lines of code must be written as the next step in continuation of the code block written above.

C#
if (FirfoxExists == true)
{
    FileStream fs = new FileStream(InstallLocation + 
                        "\\greprefs\\all.js", 
                        FileMode.Open, FileAccess.Read);
    StreamReader sr = new StreamReader(fs);
    String t;
    while ((t = sr.ReadLine()) != null)
    {
        if (t.Contains("browser.cache.disk.capacity"))
        {
            capacity = t.Substring(t.LastIndexOf(",") + 1, 
                       t.LastIndexOf(")")-1 - t.LastIndexOf(","));
            break;
        }
    }
}
else
{
    capacity = "Not Exists";
}
return capacity;

In the code above, if Firefox is installed, we open the file all.js from its install location using a FileStream, and then read the file line by line using a StreamWriter. Once the StreamWriter reads the .js file line by line, we check to see if a particular line contains the string "browser.cache.disk.capacity". Is yes, then we read the value of the cache size using the code shown above and store it in a variable. In my case, I have used a global variable "capacity". Next, we immediately break the loop. The other global variables are "FirfoxExists", "finalpath", and "InstallLocation".

License

This article, along with any associated source code and files, is licensed under The Common Development and Distribution License (CDDL)


Written By
Program Manager Tata Consultancy Services
India India
Microsoft Certified Technology Specialist: Microsoft Office SharePoint Server 2007:Configuration

Microsoft Certified Technology Specialist: SharePoint Server 2010:Development

Microsoft Certified Technology Specialist: SharePoint Server 2010:Configuring

Certified Scrum Master

Comments and Discussions

 
-- There are no messages in this forum --