如何使用C#获取不同的参数来登录.txt文件?

时间:2015-08-23 00:43:57

标签: c# logging

我正在为我的游戏制作一个应用程序,它将所有用户活动和其他变量存储在.txt文件中。我编写了部分来获取参数,还编写了一个文本文件来写下来的部分。 但我无法将这两部分联系起来。 我将在这里发布代码,注意我有需要写下来的int和字符串参数。

using UnityEngine;
using System.Collections;
using System.IO;`



public class Levels_Menu : MonoBehaviour
{

    public GUISkin skin;

    void OnGUI()
    {

        GUI.skin = skin;

        if (GUI.Button(new Rect(Screen.width / 2 - 750, Screen.height / 2 - 100, 300, 100), "EASY"))
        {
            //Application.LoadLevel("Game_Difficulty_Easy");
            ApplicationLoadLevel("Game_Difficulty_Easy");
        }

        if (GUI.Button(new Rect(Screen.width / 2 - 450, Screen.height / 2 - 100, 300, 100), "NORMAL"))
        {
            //Application.LoadLevel("Game_Difficulty_Normal");
            ApplicationLoadLevel("Game_Difficulty_Normal");
        }

        if (GUI.Button(new Rect(Screen.width / 2 - 150, Screen.height / 2 - 100, 300, 100), "HARD"))
        {
            //Application.LoadLevel("Game_Difficulty_Hard");
            ApplicationLoadLevel("Game_Difficulty_Hard");
        }

        if (GUI.Button(new Rect(Screen.width / 2 + 150, Screen.height / 2 - 100, 300, 100), "SURVIVAL"))
        {
            //Application.LoadLevel("Game_Difficulty_Survival");
            ApplicationLoadLevel("Game_Difficulty_Survival");
        }

        if (GUI.Button(new Rect(Screen.width / 2 + 450, Screen.height / 2 - 100, 300, 100), "FIND THE COLOR"))
        {
            //Application.LoadLevel("Game_Difficulty_FindTheColor");
            ApplicationLoadLevel("Game_Difficulty_FindTheColor");
        }

        if (GUI.Button(new Rect(Screen.width / 2 - 250, Screen.height / 2 - 300, 500, 100), "PLAY RANDOM LEVEL"))
        {
            //Application.LoadLevel(Random.Range(4, 8));
            ApplicationLoadLevel(Random.Range(4, 8));
        }

        if (GUI.Button(new Rect(Screen.width / 2 + 400, Screen.height / 2 - 300, 300, 100), "MAIN MENU"))
        {
            //Application.LoadLevel("Main_Menu");
            ApplicationLoadLevel("Main_Menu");
        }
    }

    private void ApplicationLoadLevel(string level)
    {
        Logger.LogThis(level);
        Application.LoadLevel(level);

    }

    private void ApplicationLoadLevel(int level)
    {
        Logger.LogThis(level);
        Application.LoadLevel(level);
    }

    public static class Logger
    {

         public static void LogThis()
    {
            string path = @"C:\temp\ExampleNew.txt";
            if (!File.Exists(path))
            {
                File.Create(path);
                TextWriter tw = new StreamWriter(path);
                tw.WriteLine();
                tw.Close(); 
            }
            else if (File.Exists(path))
            {
                TextWriter tw = new StreamWriter(path, true);
                tw.WriteLine();
                tw.Close(); 
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

答案显而易见:将public static void LogThis()重写为public static void LogThis(string text)然后,当您写入文本文件时,请使用tw.WriteLine(text)

当然,这并不支持直接写int,所以你可以

  1. 在将int作为参数或
  2. 传递时,在int上使用ToString()方法
  3. 将方法签名更改为public static void LogThis(object toWrite)
  4. 最后,我建议您将LogThis方法重构为以下内容:

    public static void LogThis(object toWrite)
    {
        string path = @"C:\temp\ExampleNew.txt";
        if (!File.Exists(path))
        {
            File.Create(path);
        }
        TextWriter tw = new StreamWriter(path, true);
        tw.WriteLine(toWrite);
        tw.WriteLine();
        tw.Close(); 
    }
    

    更新:TextWriter实施IDisposable!雅虎使用using语句确保关闭并释放流:

    public static void LogThis(object toWrite)
    {
        string path = @"C:\temp\ExampleNew.txt";
        if (!File.Exists(path))
        {
            File.Create(path);
        }
        using (TextWriter tw = new StreamWriter(path, true))
        {
            tw.WriteLine(toWrite);
            tw.WriteLine();
        }
    }