C# Preferences Fault Set Google Chrome Home Page

I tried to change Chrome Home Page, looking this url:Programmatically access the Google Chrome Home or Start page

This code does not change home page just return home page url. And I wrote some functions to change homepage and I changed home page in chrome prefrences but when I open chrome home page the same as before.

Here is my code;

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

Answers


If google chrome is synced with your email address, it will override your changes. So, your change will be written in pref file but once you load chrome, it will sync up with your account and override your changes. To see things working, turn off sync and test your code.


Need Your Help

Inheriting templated constructor from class template?

c++ templates c++11 inheritance constructor

What would be the syntax to inherit constructors (some of them are template) from a class template in C++11 ?

Same two codes , but still one of them has error in c [CODE BLOCKS]-[VS2010]

c visual-studio-2010 codeblocks

I wrote a code and it compiles perfectly in CodeBlocks 13.12 with no errors !