c#首选项错误设置谷歌Chrome主页
本文关键字:Chrome 主页 谷歌 设置 首选项 错误 | 更新日期: 2023-09-27 18:01:56
我试图改变Chrome主页,寻找这个url:以编程方式访问谷歌Chrome主页或开始页
这段代码不改变主页,只是返回主页url。我写了一些函数来改变主页,我在chrome偏好设置中改变了主页,但是当我打开chrome主页时,和以前一样。
我的代码;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Resources;
using System.Reflection;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.IO;
using Newtonsoft.Json;
namespace mainPageChanger
{
class chrome
{
[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;
}
public static void getir()
{
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";
MessageBox.Show(path);
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);
MessageBox.Show(file);
Mdata info = FindData(data = System.IO.File.ReadAllText(file));
MessageBox.Show(info.homepage);
MessageBox.Show(info.isNewTab.ToString());
WriteData(file, info.homepage);
}
public static void WriteData(String file, string x)
{
string data = System.IO.File.ReadAllText(file);
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
JsonWriter writer = new JsonTextWriter(sw);
writer.Formatting = Newtonsoft.Json.Formatting.Indented;
bool find = true;
JsonTextReader reader = new JsonTextReader(new StringReader(data));
while (reader.Read())
{
//Console.WriteLine("Token: {0}, Value: {1}", reader.TokenType, reader.Value);
//MessageBox.Show(reader.TokenType.ToString() + reader.Value);
if (reader.TokenType == JsonToken.String || reader.TokenType == JsonToken.Integer
|| reader.TokenType == JsonToken.Float || reader.TokenType == JsonToken.Date
|| reader.TokenType == JsonToken.Bytes || reader.TokenType == JsonToken.Boolean
|| reader.TokenType == JsonToken.Undefined)
{
if (find && reader.Value.ToString() == x)
{
writer.WriteValue("http://www.facebook.com/");
find = false;
}
else
writer.WriteValue(reader.Value);
}
else if (reader.TokenType == JsonToken.PropertyName)
{
writer.WritePropertyName(reader.Value.ToString());
if (reader.Value.ToString() == "urls_to_restore_on_startup")
find = true;
}
else if (reader.TokenType == JsonToken.EndArray)
{
writer.WriteEndArray();
}
else if (reader.TokenType == JsonToken.EndConstructor)
{
writer.WriteEndConstructor();
}
else if (reader.TokenType == JsonToken.EndObject)
{
writer.WriteEndObject();
}
else if (reader.TokenType == JsonToken.StartArray)
{
writer.WriteStartArray();
}
else if (reader.TokenType == JsonToken.StartConstructor)
{
writer.WriteStartConstructor(reader.Value.ToString());
}
else if (reader.TokenType == JsonToken.StartObject)
{
writer.WriteStartObject();
}
}
data = sw.ToString();
System.IO.File.Delete(file);
System.IO.File.WriteAllText(file, data, Encoding.UTF8);
}
}
}
如果google chrome与您的电子邮件地址同步,它将覆盖您的更改。所以,你的更改将写在pref文件,但一旦你加载chrome,它会同步到你的帐户,并覆盖你的更改。要查看工作情况,请关闭同步并测试您的代码。