简体   繁体   中英

How Do I save variables to a new text file so that those variables are loaded the next time the program runs?

new C#er here. I'm making a console based RPG. It's coming along quite well, but I need to find out how to save the game. I would guess that there is a way to save variables from my app into a text file that can be used to load the variables when the application is run again. Unfortunately I have no idea where to start.

Also I need a way to go to a point in the code when loading a save file.

Some of my variables include:

int xCoordinate, yCoordinate, hp, hpmax, level;

Any sample code would be greatly appreciated.

It is simple to write some variables to a text file:

TextWriter tw = new StreamWriter("SavedGame.txt");

// write lines of text to the file
tw.WriteLine(xCoordinate);
tw.WriteLine(yCoordinate);

// close the stream     
tw.Close();

And read them back in:

// create reader & open file
TextReader tr = new StreamReader("SavedGame.txt");

// read lines of text
string xCoordString = tr.ReadLine();
string yCoordString = tr.ReadLine();

//Convert the strings to int
xCoordinate = Convert.ToInt32(xCoordString);
yCoordinate = Convert.ToInt32(yCoordString);

// close the stream
tr.Close();

You can save the variables to an XML file, and load them on the next startup, a process called serialization . See here for a helper class that can serialize and deserialize most any c# object (including lists, but not dictionaries) to and from an XML file.

If there are only a few values that you want to transmit to your next console application, you can use command line parameters or a pipe .

You can use binary serialization to accomplish this fairly easily. First, create a class containing all the variables you want to write:

[Serializable]
class Data
{
    int x;
    int y;
}

Then use it as follows:

Data data = new Data();

//Set variables inside data here...

// Save data
BinaryFormatter formatter = new BinaryFormatter();
using (FileStream stream = File.OpenWrite("C:\\Temp\\bin.bin"))
{
    formatter.Serialize(stream, data);
}

My script for saving variable (hidden,encrypted,can edit,change,save,delete)

public class SavingPlugin
{

    public static void SaveVariable(string savename, TypeCode tc, object value,string Encryption_password)
    {
        string path = AppDomain.CurrentDomain.BaseDirectory + @"\" + savename + "." + tc.ToString();

        if (!File.Exists(path))
        {
            var myfile = File.Create(path);
            myfile.Close();
            string val = value.ToString();
            string encrypted = StringCipher.Encrypt(val, Encryption_password);
            File.WriteAllText(path, encrypted);
            File.SetAttributes(path, FileAttributes.Hidden);

        }
        else
        {
            string txt = "";
            try
            {
                txt = StringCipher.Decrypt(File.ReadAllText(path), Encryption_password);
                File.SetAttributes(path, FileAttributes.Normal);
                string val = value.ToString();
                string encrypted = StringCipher.Encrypt(val, Encryption_password);
                File.WriteAllText(path, encrypted);
                File.WriteAllText(path, encrypted);
                File.SetAttributes(path, FileAttributes.Hidden);
            }
            catch
            {
                MessageBox.Show("Incorrect password : " + Encryption_password + " for the variable : " + savename + "." + tc.ToString());
            }

        }
    }

    public static object GetVariable(string savename, TypeCode tc, string Encryption_password)
    {
        string path = AppDomain.CurrentDomain.BaseDirectory + @"\" + savename + "." + tc.ToString();
        File.SetAttributes(path, FileAttributes.Normal);
        string txt = "";

        try
        {
            txt = StringCipher.Decrypt(File.ReadAllText(path), Encryption_password);
            File.SetAttributes(path, FileAttributes.Hidden);
            var value = Convert.ChangeType(txt, tc);
            return value;
        }
        catch
        {
            MessageBox.Show("Incorrect password : " + Encryption_password + " for the variable : " + savename + "." + tc.ToString());
            return null;
        }


    }

    public static void DeleteVariable(string savename,TypeCode tc)
    {
        string path = AppDomain.CurrentDomain.BaseDirectory + @"\" + savename + "." + tc.ToString();
        File.SetAttributes(path,FileAttributes.Normal);
        File.Delete(path);
    }

    public static bool Exists(string savename,TypeCode tc)
    {
        string path = AppDomain.CurrentDomain.BaseDirectory + @"\" + savename + "." + tc.ToString();
        bool _true = true;

        try
        {
            File.SetAttributes(path, FileAttributes.Normal);
            File.SetAttributes(path, FileAttributes.Hidden);
        }
        catch
        {
            _true = false;
        }

        return _true;
    }
}

public static class StringCipher
{
    // This constant is used to determine the keysize of the encryption algorithm in bits.
    // We divide this by 8 within the code below to get the equivalent number of bytes.
    private const int Keysize = 256;

    // This constant determines the number of iterations for the password bytes generation function.
    private const int DerivationIterations = 1000;

    public static string Encrypt(string plainText, string passPhrase)
    {
        // Salt and IV is randomly generated each time, but is preprended to encrypted cipher text
        // so that the same Salt and IV values can be used when decrypting.  
        var saltStringBytes = Generate256BitsOfRandomEntropy();
        var ivStringBytes = Generate256BitsOfRandomEntropy();
        var plainTextBytes = Encoding.UTF8.GetBytes(plainText);
        using (var password = new Rfc2898DeriveBytes(passPhrase, saltStringBytes, DerivationIterations))
        {
            var keyBytes = password.GetBytes(Keysize / 8);
            using (var symmetricKey = new RijndaelManaged())
            {
                symmetricKey.BlockSize = 256;
                symmetricKey.Mode = CipherMode.CBC;
                symmetricKey.Padding = PaddingMode.PKCS7;
                using (var encryptor = symmetricKey.CreateEncryptor(keyBytes, ivStringBytes))
                {
                    using (var memoryStream = new MemoryStream())
                    {
                        using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
                        {
                            cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
                            cryptoStream.FlushFinalBlock();
                            // Create the final bytes as a concatenation of the random salt bytes, the random iv bytes and the cipher bytes.
                            var cipherTextBytes = saltStringBytes;
                            cipherTextBytes = cipherTextBytes.Concat(ivStringBytes).ToArray();
                            cipherTextBytes = cipherTextBytes.Concat(memoryStream.ToArray()).ToArray();
                            memoryStream.Close();
                            cryptoStream.Close();
                            return Convert.ToBase64String(cipherTextBytes);
                        }
                    }
                }
            }
        }
    }

    public static string Decrypt(string cipherText, string passPhrase)
    {
        // Get the complete stream of bytes that represent:
        // [32 bytes of Salt] + [32 bytes of IV] + [n bytes of CipherText]
        var cipherTextBytesWithSaltAndIv = Convert.FromBase64String(cipherText);
        // Get the saltbytes by extracting the first 32 bytes from the supplied cipherText bytes.
        var saltStringBytes = cipherTextBytesWithSaltAndIv.Take(Keysize / 8).ToArray();
        // Get the IV bytes by extracting the next 32 bytes from the supplied cipherText bytes.
        var ivStringBytes = cipherTextBytesWithSaltAndIv.Skip(Keysize / 8).Take(Keysize / 8).ToArray();
        // Get the actual cipher text bytes by removing the first 64 bytes from the cipherText string.
        var cipherTextBytes = cipherTextBytesWithSaltAndIv.Skip((Keysize / 8) * 2).Take(cipherTextBytesWithSaltAndIv.Length - ((Keysize / 8) * 2)).ToArray();

        using (var password = new Rfc2898DeriveBytes(passPhrase, saltStringBytes, DerivationIterations))
        {
            var keyBytes = password.GetBytes(Keysize / 8);
            using (var symmetricKey = new RijndaelManaged())
            {
                symmetricKey.BlockSize = 256;
                symmetricKey.Mode = CipherMode.CBC;
                symmetricKey.Padding = PaddingMode.PKCS7;
                using (var decryptor = symmetricKey.CreateDecryptor(keyBytes, ivStringBytes))
                {
                    using (var memoryStream = new MemoryStream(cipherTextBytes))
                    {
                        using (var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
                        {
                            var plainTextBytes = new byte[cipherTextBytes.Length];
                            var decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
                            memoryStream.Close();
                            cryptoStream.Close();
                            return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
                        }
                    }
                }
            }
        }
    }

    private static byte[] Generate256BitsOfRandomEntropy()
    {
        var randomBytes = new byte[32]; // 32 Bytes will give us 256 bits.
        using (var rngCsp = new RNGCryptoServiceProvider())
        {
            // Fill the array with cryptographically secure random bytes.
            rngCsp.GetBytes(randomBytes);
        }
        return randomBytes;
    }
}

How to use :

SavingPlugin.SaveVariable("int16",TypeCode.Int16,15,"awwdad");
MessageBox.Show(SavingPlugin.GetVariable("int16", TypeCode.Int16,"awwdad").ToString());

You can change the value of a variable already existing using SaveVariable but you need to enter the correct password

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