C#首选项故障设置谷歌浏览器主页

时间:2014-05-06 04:53:32

标签: c# google-chrome

我尝试更改Chrome主页,查看此网址:Programmatically access the Google Chrome Home or Start page

此代码不会更改主页只返回主页url。我写了一些功能来改变主页,我在chrome prefrences中更改了主页,但是当我打开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);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

如果谷歌浏览器与您的电子邮件地址同步,它将覆盖您的更改。因此,您的更改将以pref文件编写,但一旦您加载了Chrome,它就会与您的帐户同步并覆盖您的更改。要查看工作正常,请关闭同步并测试代码。