Agyapal
Agyapal

Reputation: 23

C# code to change browser download options

I am trying to do following with c#.

1) Open Firefox Browser-->Tools-->Options-->General Tab--->Downloads--->Always ask me where to save file.

I want to do this whole process in my application with c#. I want that when download window opens, the radio button in "Always ask me where to save file" option gets checked automatically.

I have tried from various links, but all is in vain.

Upvotes: 0

Views: 1985

Answers (2)

user989056
user989056

Reputation: 1274

Here is the full code, console application. Summary: preferences file is located in application roaming folder, something like this on windows 7:

C:\Users\MYNAME\AppData\Roaming\Mozilla\Firefox\Profiles\d9i9jniz.default\prefs.js

We alter this file so that it includes "user_pref("browser.download.useDownloadDir", false);"

Restart firefox, and done. Only run this application when firefox is not running.

 static void Main(string[] args)
    {
        if (isFireFoxOpen())
        {
            Console.WriteLine("Firefox is open, close it");
        }
        else
        {
            string pathOfPrefsFile = GetPathOfPrefsFile();

            updateSettingsFile(pathOfPrefsFile);
            Console.WriteLine("Done");
        }
        Console.ReadLine();
    }

    private static void updateSettingsFile(string pathOfPrefsFile)
    {
        string[] contentsOfFile = File.ReadAllLines(pathOfPrefsFile);
        // We are looking for "user_pref("browser.download.useDownloadDir", true);"
        // This needs to be set to:
        // "user_pref("browser.download.useDownloadDir", false);"
        List<String> outputLines = new List<string>();
        foreach (string line in contentsOfFile)
        {
            if (line.StartsWith("user_pref(\"browser.download.useDownloadDir\""))
            {
                Console.WriteLine("Found it already in file, replacing");
            }
            else
            {
                outputLines.Add(line);
            }
        }

        // Finally add the value we want to the end
        outputLines.Add("user_pref(\"browser.download.useDownloadDir\", false);");
        // Rename the old file preferences for safety...
        File.Move(pathOfPrefsFile, Path.GetDirectoryName(pathOfPrefsFile) +  @"\" + Path.GetFileName(pathOfPrefsFile) + ".OLD." + Guid.NewGuid().ToString());
        // Write the new file.
        File.WriteAllLines(pathOfPrefsFile, outputLines.ToArray());
    }

    private static string GetPathOfPrefsFile()
    {
        // Get roaming folder, and get the profiles.ini
        string iniFilePath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\Mozilla\Firefox\profiles.ini";
        // Profiles.ini tells us what folder the preferences file is in.
        string contentsOfIni = File.ReadAllText(iniFilePath);

        int locOfPath = contentsOfIni.IndexOf("Path=Profiles");
        int endOfPath = contentsOfIni.IndexOf(".default", locOfPath);

        int startOfPath = locOfPath + "Path=Profiles".Length + 1;
        int countofCopy = ((endOfPath + ".default".Length) - startOfPath);
        string path = contentsOfIni.Substring(startOfPath, countofCopy);

        string toReturn = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\Mozilla\Firefox\Profiles\" + path + @"\prefs.js";
        return toReturn;
    }

    public static bool isFireFoxOpen()
    {
        foreach (Process proc in Process.GetProcesses())
        {
            if (proc.ProcessName == "firefox")
            {
                return true;
            }
        }
        return false;
    }

Upvotes: 1

CodeCaster
CodeCaster

Reputation: 151664

What have you tried?

Firefox settings are stored in your profile, so I'd guess you can change the contents of the given file. Type about:config to find the setting you're looking for, I guess it's in the browser.download tree, alter it (after you made sure the browser isn't running) and you should be good to go.

Upvotes: 0

Related Questions