以编程方式访问Google Chrome主页或“开始”页面

时间:2010-10-24 12:28:24

标签: c# google-chrome

Chrome在哪里保存主页或首页网址?我想使用C#以编程方式访问它。

3 个答案:

答案 0 :(得分:31)

  

默认位置为:

     

Windows XP

     

Google Chrome: C:\Documents and Settings\<username>\Local Settings\Application Data\Google\Chrome\User Data\Default
   Chromium: C:\Documents and Settings\<username>\Local Settings\Application Data\Chromium\User Data\Default

     

Vista / 7

     

Google Chrome: C:\Users\<username>\AppData\Local\Google\Chrome\User Data\Default
   Chromium: C:\Users\<username>\AppData\Local\Chromium\User Data\Default

     

Mac OS X

     

Google Chrome: ~/Library/Application Support/Google/Chrome/Default
   Chromium: ~/Library/Application Support/Chromium/Default

     

的Linux

     

Google Chrome: ~/.config/google-chrome/Default
   Chromium: ~/.config/chromium/Default

来源:Google Chromium用户数据目录的默认位置。 (link

在我写这篇文章的时间上,这是我能想到的最短且最健壮的例子(我完全忽略了这个事实,即用户可以使用不同的位置然后默认)。必须说,它有点棘手,然后我想。

在此示例中,我尝试使用默认位置目录,并查找存储“Home”的首选项文件。它以JSon格式存储,因此我将我感兴趣的数据反序列化并打印出来。

赢7示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
//References -> Add Reference -> "System.Runtime.Serialization" Add
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;

namespace test {
    class Program {
        [DataContract]
        public class Mdata {
            [DataMember(Name = "homepage")] 
            public String homepage { get; private set; }
            [DataMember(Name = "homepage_is_newtabpage")]
            public Boolean isNewTab { get; private set; }
            public Mdata() { }
            public Mdata(String data) {
                homepage = data;
            }
        }

        public static Mdata FindData(String json) {
            Mdata deserializedData = new Mdata();
            MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json));
            DataContractJsonSerializer ser = new DataContractJsonSerializer(deserializedData.GetType());
            deserializedData = ser.ReadObject(ms) as Mdata;
            ms.Close();
            return deserializedData;
        }

        static void Main(string[] args) {
            const int LikeWin7 = 6;
            OperatingSystem osInfo = Environment.OSVersion;
            DirectoryInfo strDirectory;
            String path=null, file=null, data;

            if (osInfo.Platform.Equals(System.PlatformID.Win32NT))
                if (osInfo.Version.Major == LikeWin7)
                    path = Environment.GetEnvironmentVariable("LocalAppData") +
                        @"\Google\Chrome\User Data\Default";
            if (path == null || path.Length == 0)
                throw new ArgumentNullException("Fail. Bad OS.");
            if (!(strDirectory = new DirectoryInfo(path)).Exists)
                throw new DirectoryNotFoundException("Fail. The directory was not fund");
            if (!new FileInfo(file = Directory.GetFiles(strDirectory.FullName, "Preferences*")[0]).Exists)
                throw new FileNotFoundException("Fail. The file was not found.", file);

            Mdata info = FindData(data = System.IO.File.ReadAllText(file));
            Console.WriteLine(info.homepage);
            Console.WriteLine(info.isNewTab);
        }
    }
}

Me的输出示例:

chrome://newtab
True

希望我获得至少1票:P

答案 1 :(得分:5)

在Windows 7(我猜Vista)上使用默认安装,它存储在文件中:

%USERPROFILE%\AppData\Local\Google\User Data\Default\Preferences

在Windows 2003(和XP)上:

%USERPROFILE%\Local Settings\Application Data\Google\Chrome\User Data\Default\Preferences

要查找的属性名称为:homepage

答案 2 :(得分:0)

您需要的首选项文件的一小部分。

},
      "homepage": "http://www.google.com/",
      "homepage_is_newtabpage": true,
      "pinned_tabs": [ {