Reading TextFile Line by Line from "Documents"

Hello Forum User,

I Searching for a script witch can read a textfile from the following path: System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments) + “/My Games/test/demo/Saves/”

Also I want that the user can choose the file to load because the file
in my demo is name like this: (filePath + “/save_” + dateTime (dateTime = DateTime.Now.ToString(“dd-MM-yyyy_HH-mm-ss”);))

Hope you can help me!

Read a text file from disk line by line?

Sounds like you need a StreamReader.

MSDN has it well documented with examples of how to use it:

I tried it with that but it doesn’s work. Can Any Body post the part of the Code here to choose a save file and read it line by line?

Well you can’t just copy paste it and have it work.

What part of it are you having a hard time with? We’re not just going to write code for you. We’re here to help, not just do.

You know… teach a man to fish vs give a man a fish.

Yes I know.

But how can i make it that the user can choose the save file beacause there can be 1000files or just 2 and there have all different names. I can’t find it there.

Use Directory.GetFiles(string, string) to get a specific files with the extension of your choice.

This is not what i’m looking for.

I think it is called File Dialog or something like this.
How can I open it and how can I get the string from the path in to a string.


At the moment my Loadding System looks like that but i like to have every readed String in his own String. How can i get every single String from the Array to a seperated string?

In c it is with line[4] or so but this doesen’t work in C#

string[ ] lines;
var list = new List();
var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
using (var streamReader = new StreamReader(fileStream))
{
string line;
while ((line = streamReader.ReadLine()) != null)
{
list.Add(line);
}
}
lines = list.ToArray();

ps. Sorry for my bad english. I’m from germany and doesen’t write/speak much english.

You can’t use the normal FileDialog from within Unity, but you can use this open source asset to do the job.

https://github.com/yasirkula/UnitySimpleFileBrowser

This is with java but i search this for c#. I don’t like java!

No, that’s mostly in C#.

There is some Java to it as a native plugin for Android support.

But the bulk of the project in the ‘Assets/Plugin’ folder is C# scripts:
https://github.com/yasirkula/UnitySimpleFileBrowser/tree/master/Assets/Plugins/SimpleFileBrowser/Scripts

1 Like

At the moment my Loadding System looks like that but i like to have every readed String in his own String. How can i get every single String from the Array to a seperated string?

string[ ] lines;
var list = new List();
var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
using (var streamReader = new StreamReader(fileStream))
{
string line;
while ((line = streamReader.ReadLine()) != null)
{
list.Add(line);
}
}
lines = list.ToArray();

First off, use code tags:

Next, if all you wanted to do is read all the lines into an array, a method exists for doing that:

Thing is, I didn’t get the impression that’s what you were trying to do… usually one wants to do something with every line of text. You also were asking about a file browser.

I try to read the hole text from a file into the script and save it in a float var to use it in the game.


This is the File Browser where you can choose the saved file. Then the file from the choosen path in the browser schould be read by the script and the text from the file schould be saved in a float or bool var to use it for the game.

I highly recommend you to use BinaryFormatter (in case you want to serialize as binary), or XMLSerializer (as text) for such use-cases.
Otherwise you’d have to parse each object and variable manually, which will definitely lead to bugs and some major headache.

As for the file browser - you can re-create that via Unity UI, and some scripts. It’s not that hard.

I like to save and then load this float/int/bool var:

//float
save_ecount = GlobalCountSystem.ECount;
save_autoecount = GlobalCountSystem.AutoECount;
save_mcount = GlobalCountSystem.MCount;
save_ebuttoncount = GlobalCountSystem.EButtonCount;
save_fullecount = GlobalCountSystem.FullECount;

//int
save_ccount = GlobalCountSystem.Ccount;

//float
save_upgradeprice1 = ShopSystem.UpgradePrice1;
save_upgradeprice2 = ShopSystem.UpgradePrice2;
save_upgradeprice3 = ShopSystem.UpgradePrice3;
save_upgradeprice4 = ShopSystem.UpgradePrice4;
save_upgradeprice5 = ShopSystem.UpgradePrice5;
save_upgradeprice6 = ShopSystem.UpgradePrice6;

//float
save_upgradelevel1 = ShopSystem.UpgradeLevel1;
save_upgradelevel2 = ShopSystem.UpgradeLevel2;
save_upgradelevel3 = ShopSystem.UpgradeLevel3;
save_upgradelevel4 = ShopSystem.UpgradeLevel4;
save_upgradelevel5 = ShopSystem.UpgradeLevel5;
save_upgradelevel6 = ShopSystem.UpgradeLevel6;

//Bool
save_c1time = AchivementsSystem.c1time;
save_get20e = AchivementsSystem.get20e;
save_opentheshop = AchivementsSystem.opentheshop;
save_sell1e = AchivementsSystem.sell1e;
save_screenshoot = AchivementsSystem.screenshoot;
save_get100e = AchivementsSystem.get100e;
save_otp = AchivementsSystem.otp;
save_getonfire = AchivementsSystem.getonfire;
save_pausethegame = AchivementsSystem.pausethegame;

This is my Save script to write the file.

public void onSave(){
        dateTime = DateTime.Now.ToString("dd-MM-yyyy_HH-mm-ss");

        filePath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "/My Games/GameTest/Saves/";

        try
        {
            if (!Directory.Exists(filePath))
            {
                Directory.CreateDirectory(filePath);
            }

        }
        catch (IOException ex)
        {
            Console.WriteLine(ex.Message);
        }

        File.WriteAllText(filePath + "/save_" + dateTime + ".save", save_ecount + "\n" + ... + "\n");
}

this is my Load script where i want to load the file data into the var. And this is the Problem

public void onLoad(){
        filePath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments) + "/My Games/GameTest/Saves/save-test.save";

        var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
        using (var streamReader = new StreamReader(fileStream))
        {
            while ((line = streamReader.ReadLine()) != null)
            {
                // process the line
                Debug.Log(line);
            }
        }
}