我想要做的是更改Google Chrome瀏覽器的Preferences
文件中的兩個值。我現在使用的代碼是低於如何修改JSON格式文件?
[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
}
什麼,我想現在要做的就是info.homepage,info.isNewTab和info.isSynced的值更改爲其他值,要像(在首選項文件):
"homepage": "http://www.myWebsite.com/",
"homepage_is_newtabpage": true,
...
"keep_everything_synced": false,
我試圖directrly設置新值是這樣的:
info.homepage = "www.mysite.com";
info.isNewTab = true;
info.isSynced = false;
,但沒有機會!
如何爲上述三個對象分配新值?
你是如何將數據寫回磁盤的? – svick 2012-04-07 09:30:38