用于更改浏览器下载选项的C#代码

时间:2012-10-10 07:19:08

标签: c# firefox browser

我正在尝试使用c#

1)打开Firefox浏览器 - >工具 - >选项 - >常规标签--->下载--->总是问我在哪里保存文件。

我想在c#的应用程序中完成整个过程。我希望在下载窗口打开时,"Always ask me where to save file"选项中的单选按钮会自动检查。

我试过各种各样的链接,但一切都是徒劳的。

2 个答案:

答案 0 :(得分:1)

这是完整的代码,控制台应用程序。 摘要:首选项文件位于应用程序漫游文件夹中,类似于Windows 7上的内容:

C:\ Users \用户MYNAME \应用程序数据\漫游\ Mozilla的\火狐\概况\ d9i9jniz.default \的prefs.js

我们更改此文件,使其包含“user_pref(”browser.download.useDownloadDir“,false);”

重启firefox,完成了。仅在firefox未运行时运行此应用程序。

 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;
    }

答案 1 :(得分:0)

你有什么尝试?

Firefox设置存储in your profile,所以我猜您可以更改给定文件的内容。键入about:config以查找您要查找的设置,我猜它在browser.download树中,更改它(在您确定浏览器未运行之后)并且您应该很高兴。