简体   繁体   中英

How to check if user modified a file since the last time the application was run

I am making a simple idle game of sorts as a side project. I currently have the game saving the data to a text file so that the next time the application runs it loads the data and calculates the score earned while offline. I want to make sure the user cannot edit this file to achieve a higher score. My first idea was to encrypt the file, but that seemed a little too advanced for me so I decided to just reset the score if the user tries to edit the file. Here is what I have.

Here are the two DateTime variables I'm using.

DateTime dtPast = DateTime.Now; //The date since last login
DateTime lastModified = System.IO.File.GetLastWriteTime("\\SavedGame.txt");

dtPast is updated every time the game saves, which is every 100 milliseconds. (I have tried from 1 millisecond to 1 second already, none work)

I then test if the two dates match, and if they don't then it resets all variables.

            if (lastModified != dtPast)
            {
                //button
                intButton = 1;
                decNumber = 0;
                intAutoClicks = 0;


                //items
                //item 1
                intItem1 = 0;
                intItem1Lvl = 0;
                intItem1Prc = 10;

                //item 2
                intItem2 = 0;
                intItem2Lvl = 0;
                intItem2Prc = 250;

                //item 3
                intItem3 = 0;
                intItem3Lvl = 0;
                intItem3Prc = 5000;

                //item 4
                intItem4 = 0;
                intItem4Lvl = 0;
                intItem4Prc = 100000;

                //item 5
                intItem5 = 0;
                intItem5Lvl = 0;
                intItem5Prc = 1000000;

                //item 6
                intItem6 = 0;
                intItem6Lvl = 0;
                intItem6Prc = 25000000;

                //item 7
                intItem7 = 0;
                intItem7Lvl = 0;
                intItem7Prc = 1000000000;

                //item 8
                intItem8 = 0;
                intItem8Lvl = 0;
                intItem8Prc = 100000000000;

                //upgrades

                //upgrade 1
                // upgrade 1 is intButton
                intUpgrade1Lvl = 1;
                intUpgrade1Prc = 100;

                //upgrade 2
                intUpgrade2 = 0;
                intUpgrade2Lvl = 0;
                intUpgrade2Prc = 1000;

                //upgrade 3
                intUpgrade3 = 0;
                intUpgrade3Lvl = 0;
                intUpgrade3Prc = 100000;

                //upgrade 4
                intUpgrade4 = 0;
                intUpgrade4Lvl = 0;
                intUpgrade4Prc = 1500000;

                //Upgrade 5
                // upgrade 5 is intButton
                intUpgrade5Lvl = 0;
                intUpgrade5Prc = 10000000;
            }

The only problem is that this does not seem to be working, and I'm not sure why. As I've said, I've tried getting the dtPast down to the milliseconds, but it doesn't work. I've also tried requiring them to be within a second of eachother but that doesn't work either.

Mathias answered it in the comments.

if(dtPast.AddMilliseconds(-100) < lastModified)

didn't work so I tried

if(dtPast.AddMilliseconds(1000) < lastModified)

and it worked great.

You can use this library to zip the file with a password and the user cannot change it since they do not know the password. Make sure you don't make the password 1234 ;)

using (var zipped = new ZipFile())
{
    zipped.Password= "Vx*!~zSecret!!";
    zipped.AddFile("Score.txt");
    zipped.Save("Archive.zip"); 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM