gpt4 book ai didi

c# - 以编程方式访问 Google Chrome 主页或起始页

转载 作者:太空宇宙 更新时间:2023-11-03 16:44:58 32 4
gpt4 key购买 nike

Chrome 将主页或起始页 URL 保存在哪里?我想使用 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

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

就我写这篇文章所花的时间而言,这是我能想到的最短、最可靠的示例(我完全忽略了一个事实,即用户可以使用与默认位置不同的位置)。必须说,这有点棘手,然后我想。

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

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

我的示例输出:

chrome://newtab
True

希望我至少得到 1 个赞成票 :P

关于c# - 以编程方式访问 Google Chrome 主页或起始页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6332287/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com