Question

I am creating a game with c# and i have occurred a problem. I want my game to save curtain labels to a txt file here ill give you a example:

Label1.Text = "Character Name: "
Label2.Text = "Level: "
Label3.Text = "Exp: "

Now what i wanted to do was to retrieve "Character Name: [Name]" From a txt file? But i also wanted to save the name to a txt file when you exit the game. so here might be a better example:

I want to retrieve Level: [LVL] From a txt file and when the gamer has finished and exit my game i want it to overwrite the existing line that was there with there new level?

I think I need to use StringReader or StringWriter.

Was it helpful?

Solution 2

You may try to use XML for the settings file. Here is a very simple example:

Create a new XML file, fill it with the character name and save:

XElement x = new XElement("Settings");
x.Add(new XElement("CharacterName", "John Doe"));
x.Save("1.xml");

Load the saved xml and print the output:

XElement loaded = XElement.Load("1.xml");
Console.WriteLine(loaded.Element("CharacterName").Value);

Here is an MSDN article to start with:LINQ to XML


If .NetFramework 3.5 and higher is not available, you may use classes from System.Xml namespace. See

  1. XML in .NET: .NET Framework XML Classes and C# Offer Simple, Scalable Data Manipulation
  2. Manipulate XML data with XPath and XmlDocument
  3. Writing XML with the XmlDocument class

...just google with C# xmldocument.

OTHER TIPS

Here's a quick sample using XML serialization with a data model. Make sure you have a using System.Xml.Serialization; namespace import line at the top of your file.

Declare the data you wish to save/load as a simple class, for example:

public class SaveData
{
    public string CharacterName { get; set; }
    public int Level { get; set; }
    public int Experience { get; set; }
}

When you want to save, build that data model:

SaveData saveState = new SaveData()
{
    CharacterName = myCharacter.Name,
    Level = myCharacter.Level,
    Experience = myCharacter.Experience
};

To save to a file, use the XmlSerializer class and open a stream to a file to serialize it to:

public void SaveStateToFile(SaveData state)
{
    XmlSerializer serializer = new XmlSerializer(typeof(SaveData));
    using (TextWriter writeFileStream = new StreamWriter(@"C:\savefile.xml"))
    {
        serializer.Serialize(writeFileStream, state);
    }
}

That will create an xml file with the following content for example:

<?xml version="1.0" encoding="utf-8"?>
<SaveData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <CharacterName>John</CharacterName>
  <Level>10</Level>
  <Experience>9001</Experience>
</SaveData>

So that should be pretty easy to edit if needed. To load the data back:

public SaveData LoadStateFromFile()
{
    XmlSerializer serializer = new XmlSerializer(typeof(SaveData));
    using (FileStream readFileStream = new FileStream(@"C:\savefile.xml", FileMode.Open, FileAccess.Read, FileShare.Read))
    {
        return (SaveData)serializer.Deserialize(readFileStream);
    }
}

That should give you a nice type-safe data model to then rebind your GUI as desired:

SaveData loadedData = LoadStateFromFile();
Console.WriteLine(loadedData.CharacterName); //John
Console.WriteLine(loadedData.Level); //10
Console.WriteLine(loadedData.Experience); //9001

Label1.Text = "Character Name: " + loadedData.CharacterName;
Label2.Text = "Level: " + loadedData.Level;
Label3.Text = "Exp: " + loadedData.Experience;

You can try SQLite. It's database that save in text file. You will have capability to organize your data better than using your own plain text.

Another option is to use old INI files. Here is a great An INI file handling class using C# example that will help you to deal with that. Here is an example of using it:

Create a INIFile like this

INIFile ini = new INIFile("C:\\test.ini");

Use IniWriteValue to write a new value to a specific key in a section or use IniReadValue to read a value FROM a key in a specific Section.

This is the code i ended up using and it works fine:

XmlDocument xmlDoc = new XmlDocument();
XmlNode rootNode = xmlDoc.CreateElement("Info");
xmlDoc.AppendChild(rootNode);

XmlNode userNode = xmlDoc.CreateElement("Username");
userNode.InnerText = Username.Text;
rootNode.AppendChild(userNode);

xmlDoc.Save(gameFolder + Username.Text + ".sav");

I used the sources from @Konstantin Vasilcov

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top