1

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

我试过使用设置,但有一个大问题:如果文件名被更改或文件被转移到其他地方,设置就会丢失。

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

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

并且请不要推荐在线服务器,因为我正在开发一款单人游戏,而不会跟踪玩家。

4

5 回答 5

5

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

请参阅二进制格式化程序的 MSDN 文档。我在这里复制了一些相关代码。

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);
    }
}
于 2013-11-07T16:31:47.783 回答
3

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

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

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

于 2013-11-07T16:28:07.730 回答
1

唯一不受用户有意更改存储在其计算机上的数据、由于更换机器等而丢失数据的真正选择是根本不将数据存储在他们的计算机上。拥有您托管的数据库或其他服务器,用户通过网络连接到这些服务器,为他们存储数据。

于 2013-11-07T16:28:03.943 回答
1

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

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

Windows 注册表...

有很多地方可以存储信息,只有经验才能真正教你把什么放在哪里……要做出明智的猜测,你需要熟悉所有的方法……

于 2013-11-07T16:27:38.513 回答
0

您可以尝试使用独立存储

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

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

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

于 2013-11-07T16:30:16.557 回答