C# - 保存程序的变量

时间:2013-11-07 16:23:55

标签: c# variables save

我现在正在使用C#(这是一个控制台应用程序)制作游戏,并且需要保存其变量。

我尝试过使用设置,但是有一个很大的问题:如果文件名已更改或文件被转移到其他地方,则设置将丢失。

那么什么是设置保存变量并在应用程序中稍后检索它们的好方法呢?

编辑:我想将变量保存到文本文件中并稍后检索,是否可能?如果是,那怎么办?

请不要建议在线服务器,因为我正在制作单人游戏,而不记录任何玩家的踪迹。

5 个答案:

答案 0 :(得分:3)

如果您的应用结构本质上是动态的,那么它的名称可以更改,位置可以更改(即使,说实话,不要理解背后的原因)我能看到的唯一可能是在外部源中进行中继,以检索或存储您的配置信息。

简而言之:在某个地方设置一个包含您的应用配置数据的服务器,并在首次启动时尝试访问该服务器,从中加载文件,读取数据。如果失败,只需加载默认信息。

好的候选人可以是:DropBoxSkyDriveGoogleDriveBox ...为他们中的任何一个找到合适的C#API,您需要存储/读取数据。我唯一能引起你注意的解决方案是许可。请密切关注它,并确保可以以您决定使用它的方式在您的应用程序中使用它。

答案 1 :(得分:3)

存储固定类型数据的一种简单方法是使用BinaryFormatter类进行序列化。

请参阅MSDN documentation for Binary Formatter。我在这里复制了一些相关的代码。

using System;
using System.IO;
using System.Collections;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;

void SaveData() 
{
    // Create a hashtable of values that will eventually be serialized.
    Hashtable addresses = new Hashtable();
    addresses.Add("Jeff", "123 Main Street, Redmond, WA 98052");
    addresses.Add("Fred", "987 Pine Road, Phila., PA 19116");
    addresses.Add("Mary", "PO Box 112233, Palo Alto, CA 94301");

    // To serialize the hashtable and its key/value pairs,   
    // you must first open a stream for writing.  
    // In this case, use a file stream.
    FileStream fs = new FileStream("DataFile.dat", FileMode.Create);

    // Construct a BinaryFormatter and use it to serialize the data to the stream.
    BinaryFormatter formatter = new BinaryFormatter();
    try 
    {
        formatter.Serialize(fs, addresses);
    }
    catch (SerializationException e) 
    {
        Console.WriteLine("Failed to serialize. Reason: " + e.Message);
        throw;
    }
    finally 
    {
        fs.Close();
    }
}


void LoadData() 
{
    // Declare the hashtable reference.
    Hashtable addresses  = null;

    // Open the file containing the data that you want to deserialize.
    FileStream fs = new FileStream("DataFile.dat", FileMode.Open);
    try 
    {
        BinaryFormatter formatter = new BinaryFormatter();

        // Deserialize the hashtable from the file and  
        // assign the reference to the local variable.
        addresses = (Hashtable) formatter.Deserialize(fs);
    }
    catch (SerializationException e) 
    {
        Console.WriteLine("Failed to deserialize. Reason: " + e.Message);
        throw;
    }
    finally 
    {
        fs.Close();
    }

    // To prove that the table deserialized correctly,  
    // display the key/value pairs. 
    foreach (DictionaryEntry de in addresses) 
    {
        Console.WriteLine("{0} lives at {1}.", de.Key, de.Value);
    }
}

答案 2 :(得分:1)

将值保存到平面文件中......

将值存储在XML文件或数据库文件中......

Windows注册表...

有许多地方可以存储信息,只有经验才能真正教会你放在哪里......为了做出明智的猜测,你需要熟悉所有的方法......

答案 3 :(得分:1)

唯一不容易被用户故意更改存储在其计算机上的数据,由于更换机器等而导致丢失的实际选项将不会将数据存储在他们的计算机上。拥有一个您托管的数据库或其他服务器,用户通过网络连接到这些服务器,为他们存储数据。

答案 4 :(得分:0)

您可以尝试使用Isolated Storage

  

独立存储不适用于Windows应用商店应用。相反,使用   Windows.Storage命名空间中的应用程序数据类   包含在Windows运行时API中以存储本地数据和文件。

您也可以尝试使用XML文件存储用户设置,然后将其存储在 SpecialFolder.ApplicationData 目录中。

您还可以使用app.config文件来保存应用程序级设置

相关问题