0

What i am trying to do is to change the default homepage for Mozilla Firefox, I know that the homepage is stored in the following path ( In my computer )

C:\\Users\\JohnnyB\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\jmwb4bnw.default\\prefs.js

The problem is that the jmwb4bnw.default folder has a different name in each computer, which may lead my file to not work on other computer due to different folder name. For me, everything is Ok, but for other computer it doesn't work because of the wrong file path.

My question is how can i access the jmwb4bnw.default folder regardless to it's current name, this folder is a unique folder under Profiles folder.

The snippest code i am using is: (It works only on my computer)

string ff_filepath = "C:\\Users\\Sandbox\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\jmwb4bnw.default\\prefs.js";
string myLine = "user_pref('browser.startup.homepage', 'www.monde-presse.com');";


StreamWriter sw;
sw = File.AppendText(ff_filepath);
sw.WriteLine(myLine);
sw.Close();

How to make the code above works on every computer ? Or in such language, how to tell C# to access the jmwb4bnw.default folder regardless to it's name..

Note : The \Profiles\ folder contain no files, and Only one folder named jmwb4bnw.default, maybe there is someway to tell C# to access the subfolder which is found on Profiles folder regardless to it's current name ?

Finally : How can i make the above code snippest to work on every Computer ?

(In the example above, we assume that default drive is always C, and user profile is always JohnnyB)

4

3 回答 3

3

您可以使用 GetDirectories() 函数:

string userName = "Sandbox";
string[] folders = Directory.GetDirectories("C:\\Users\\" + userName  + "\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\", "*.default");

文件夹将包含所有以.default结尾的子目录

在这种情况下,文件夹 [0]将是

C:\Users\Sandbox\AppData\Roaming\Mozilla\Firefox\Profiles\jmwb4bnw.default

于 2012-04-06T14:18:03.227 回答
0

我想您只期望一个配置文件?否则,您将遇到另一个问题:查找与用户配置文件对应的目录。

在这种情况下,只需列出所有目录并取第一个。这应该对您有所帮助:http: //msdn.microsoft.com/en-us/library/6ff71z1w.aspx

于 2012-04-06T14:17:36.027 回答
0

要获取 mozilla firefox 的默认配置文件目录,您应该在 csharp 中使用此代码。

string[] folders =      Directory.GetDirectories(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Mozilla\\Firefox\\Profiles\\", "*.default");
string defaultprofile=folder[0];
于 2012-07-18T05:59:12.930 回答