0

What i want to do is to change two values from the Preferences File of Google Chrome Browser. The code i am using now is the below

[DataContract]
public class Mdata
{
    [DataMember(Name = "homepage")]
    public String homepage { get; private set; }
    [DataMember(Name = "homepage_is_newtabpage")]
    public Boolean isNewTab { get; private set; }
    [DataMember(Name = "keep_everything_synced")]
    public Boolean isSynced { 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;
}

private void button1_Click(object sender, EventArgs e)
{
    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); // show me http://www.google.com
    Console.WriteLine(info.isNewTab); // prints false
    MessageBox.Show(info.isSynced); // prints true
    }

What i want to do now is to change the values of info.homepage, info.isNewTab and info.isSynced to other values, to be something like (in the preferences file) :

"homepage": "http://www.myWebsite.com/",
"homepage_is_newtabpage": true,
...
"keep_everything_synced": false,

I tried to directrly set new values like this :

 info.homepage = "www.mysite.com";
 info.isNewTab = true;
 info.isSynced = false;

But with no chances!

How can i assign new values to the three objects above?

svick
  • 236,525
  • 50
  • 385
  • 514
Rafik Bari
  • 4,867
  • 18
  • 73
  • 123

2 Answers2

2

The code you posted has two problems:

  1. You never write the data back to disk.
  2. You have only mapped the three properties you are interested in. This means only those will get deserialized. And when you change them and write it back to disk only those properties will be serialized as all the other data hasn't made it into your data structure.

There are several possible solutions to your problem:

  1. Completely map out the preferences file in your Mdata structure. This is apparently a bit painful as the preferences file is quite large.
  2. The accepted answer to this question shows how to de/serialize an arbitrary JSON object into a dictionary.
  3. Do not use JSON deserialization at all. Just open the file read it line by line and use regular expressions to match the settings you want. Replace the value when you find one. And write each line back (modified or unmodified).
Community
  • 1
  • 1
ChrisWue
  • 18,612
  • 4
  • 58
  • 83
1

Once you have the deserialized object, as you have done here:

Mdata info = FindData(data = System.IO.File.ReadAllText(file));

Assign the values you want to those properties:

 info.homepage = "www.mysite.com";
 info.isNewTab = true;
 info.isSynced = false;

Then serialize the object back to the file, overwriting it. This is the missing bit - you never write the changes beck to disk.

DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Mdata));
using(var fs = new FileStream(filePath, FileMode.Create)
{
  ser.WriteObject(fs, info);
}
Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • This will remove all old data and add only this three data members to the file, is there someway to modify their values without removing all other members ? – Rafik Bari Apr 07 '12 at 10:16
  • @ShikataGaNai - What do you mean? Other data members will retain their values from when you deserialized them. Did you even try this? – Oded Apr 07 '12 at 15:07