Click here to Skip to main content
Click here to Skip to main content

Programmatically detecting browser cache size for Firefox using C#

By , 10 Jul 2008
Rate this:
Please Sign up or sign in to vote.

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:

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:

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:

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:

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:

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.

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)

About the Author

Neeraj Kr
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 --
| Advertise | Privacy | Mobile
Web03 | 2.8.140415.2 | Last Updated 10 Jul 2008
Article Copyright 2008 by Neeraj Kr
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid