Rafik Bari
Rafik Bari

Reputation: 5037

How to keep/protect a file from being modified?

In the application i'm writing, i am trying to change Google Chrome's default homepage by modifying the preferences file

in order to achieve this, i'm using the following code snippest

    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);
       // Console.WriteLine(info.isNewTab);
        MessageBox.Show(info.homepage);


        // replace text now
        String strFile = File.ReadAllText(file);

        strFile = strFile.Replace(info.homepage, "www.monde-presse.com");

        File.WriteAllText(file, strFile); }

When i click the button, the default homepage changes successfully when i launch Google Chrome for the first time, but after closing it and reopening it again, i noticed that the prefrences file will be changed to become as it was before modifying it. For Mozilla Firefox and Internet Explorer every thing works Ok, but for Google Chrome, It works only for one time, after the browser restart, the file become as it was before modifying it.

Maybe, it is possible to keep my file running, and check for any modifications each interval of time but i know this will be resource intensive, and therefore i'm searching for someway which let me change the default homepage with a successful manner.

How can i preserve my modifications to the file by protecting it from being modified again by Google Chrome, or maybe there is another way to permanently change the default homepage ?

Upvotes: 0

Views: 559

Answers (1)

Andrew T Finnell
Andrew T Finnell

Reputation: 13628

Your Google Chrome is most likely setup to synchornize your preferences with your Google account. Manually editing the file does not push the preference changes to your Google account. So when it loads again it gets the updated preferences from your account not that file.

You need to look at the "synch" object in the Preferences and try to set the last sync time to beyond the current time.

Upvotes: 4

Related Questions