编程方式访问谷歌浏览器主页或起始页 [英] Programmatically access the Google Chrome Home or Start page
问题描述
在哪里Chrome浏览器保存主页或启动页面的网址?我想以编程方式访问它使用C#。
默认位置为:
的Windows XP
谷歌浏览器:
C:\\ Documents和Settings \\<用户名> \\本地设置\\应用数据\\谷歌\\铬\\用户数据\\默认
结果
铬:C:\\ Documents和Settings \\<用户名> \\本地设置\\应用数据\\铬\\用户数据\\默认
Vista / 7的
谷歌浏览器:
C:\\用户\\<用户名> \\应用程序数据\\本地\\谷歌\\铬\\用户数据\\默认
铬:C:\\用户\\<用户名> \\应用程序数据\\本地\\铬\\用户数据\\默认
的Mac OS X
谷歌浏览器:
〜/库/ Application Support /谷歌/铬/默认
结果
铬:〜/库/ Application Support /铬/默认
的Linux
谷歌浏览器:
〜/ .config文件/谷歌铬/默认
结果
铬:〜/ .config文件/铬/默认
块引用>来源:谷歌铬用户数据目录默认位置。 (链接)
在的时候,我在写这个花了多少,这是我能想到的(我完全忽略了一个事实,即用户可以使用不同的位置,然后默认)最短和最强大的例子。必须说,这是有点麻烦,那么我想。
在这个例子中,我尝试使用默认位置目录,并且发现,其中的主页被存储在preference文件。它存储在 JSON格式的,所以我反序列化,我很感兴趣的数据,并打印出来。
Win 7的示例:
使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;
使用System.IO;
//参考 - >添加引用 - > System.Runtime.Serialization添加
使用System.Runtime.Serialization;
使用System.Runtime.Serialization.Json;命名空间测试{
类节目{
[DataContract]
公共类MDATA {
[数据成员(NAME =主页)]
公共字符串的主页{搞定;私人集; }
[数据成员(NAME =homepage_is_newtabpage)]
公共布尔isNewTab {搞定;私人集; }
公共MDATA(){}
公共MDATA(字符串数据){
首页=数据;
}
} 公共静态MDATA FindData(JSON字符串){
MDATA deserializedData =新MDATA();
MemoryStream的毫秒=新的MemoryStream(Encoding.UTF8.GetBytes(JSON));
DataContractJsonSerializer SER =新DataContractJsonSerializer(deserializedData.GetType());
deserializedData = ser.ReadObject(毫秒),为MDATA;
ms.Close();
返回deserializedData;
} 静态无效的主要(字串[] args){
const int的LikeWin7 = 6;
OperatingSystem的OSINFO = Environment.OSVersion;
DirectoryInfo的strDirectory;
路径字符串=空,文件= NULL,数据; 如果(osInfo.Platform.Equals(System.PlatformID.Win32NT))
如果(osInfo.Version.Major == LikeWin7)
PATH = Environment.GetEnvironmentVariable(LOCALAPPDATA)+
@\\谷歌\\铬\\用户数据\\默认;
如果(路径== NULL || path.Length == 0)
抛出新的ArgumentNullException(故障坏的操作系统。);
如果(!(strDirectory =新DirectoryInfo的(路径))。存在)
抛出新DirectoryNotFoundException(故障目录没有资金。);
如果(!新的FileInfo(文件= Directory.GetFiles(strDirectory.FullName,preferences *)[0])。存在)
抛出新的FileNotFoundException异常(失败的文件没有被发现。文件); MDATA信息= FindData(数据= System.IO.File.ReadAllText(文件));
Console.WriteLine(info.homepage);
Console.WriteLine(info.isNewTab);
}
}
}输出示例对我来说:
镀铬:// newtab
真正我希望至少获得1票起来:P
Where does Chrome save the Home or Start page URL? I want to access it programmatically using C#.
解决方案Default locations are:
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
Source: Google Chromium user data directory default locations. ( link )
In amount of time I spent on writing this, this was the shortest and most robust example I could think of (I completely ignored the fact, that user could use different location then default). Must say, it was bit trickier, then I thought.
In this example, I try using the default location directory, and finding the preference file where the "Home" is stored. It is stored in JSon format, so I deserialize the data that I am interested, and print it out.
Win 7 Example:
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); } } }
Example output for Me:
chrome://newtab True
Hope I get at least 1 up vote :P
这篇关于编程方式访问谷歌浏览器主页或起始页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!