加载多个文件异常错误C#

时间:2015-02-09 20:24:11

标签: c# exception readfile

我正在制作游戏扫雷,我正在尝试实现highScores功能。我正在尝试加载3个不同的文件(每个文件保存3个难度设置中的每一个的高分)到3个不同的richTextBox' s。当我运行该应用并点击“高分”时菜单条中的选项卡第一次工作。但是,如果我玩游戏然后尝试访问高分形式,我会收到异常错误 -

  

未处理的类型' System.IO.IOException'发生在   mscorlib.dll中

     

其他信息:进程无法访问该文件   ' C:\用户\ jzcon_000 \复印\视觉   工作室\项目\分配1 \分配1 \ BIN \调试\ highScoresMed.txt'   因为它正被另一个进程使用

这是打电话的地方

    private void highScoresToolStripMenuItem_Click(object sender, EventArgs e)
    {

        Minesweeper.HighSc highScore = new Minesweeper.HighSc();
        highScore.read();
        highScore.Show();   
    }

这是我的HighSc类中的方法

    public void read()
    {
        StreamReader readerE = File.OpenText("highScoresEasy.txt");
        StreamReader readerM = File.OpenText("highScoresMed.txt");
        StreamReader readerH = File.OpenText("highScoresHard.txt");

        if (readerE != null)
        {
            string readEasy = File.ReadAllText("highScoresEasy.txt");
            richTextBox1.Text = readEasy;
        }
        readerE.Close();

        if (readerM != null)
        {
            string readMed = File.ReadAllText("highScoresMed.txt");
            richTextBox2.Text = readMed;
        }
        readerM.Close();

        if (readerH != null)
        {
            string readHard = File.ReadAllText("highScoresHard.txt");
            richTextBox3.Text = readHard;
        }
        readerH.Close();
    }

继续保存高分类

namespace Minesweeper
{
 class Save
 {
    int diff, hr, min, sec;
    string player;

    public Save(int difficulty, int hour, int minute, int second, string playerN)
    {
        diff = difficulty;
        hr = hour;
        min = minute;
        sec = second;
        player = playerN;
    }

    public void save()
    {
        StreamWriter writerEasy = new StreamWriter("highScoresEasy.txt", true);
        StreamWriter writerMed = new StreamWriter("highScoresMed.txt", true);
        StreamWriter writerHard = new StreamWriter("highScoresHard.txt", true);

        if (diff == 1)
        {
            writerEasy.WriteLine("Time: " + hr + ":" + min + ":" + sec + " " + "Name: " + player);
            writerEasy.Close();
        }
        else if (diff == 2)
        {
            writerMed.WriteLine("Time: " + hr + ":" + min + ":" + sec + " " + "Name: " + player);
            writerMed.Close();
        }
        else if (diff == 3)
        {
            writerHard.WriteLine("Time: " + hr + ":" + min + ":" + sec + " " + "Name: " + player);
            writerHard.Close();
        }

    }
}

}

1 个答案:

答案 0 :(得分:1)

因此,当您将难度级别设置为1然后保存时,您可以打开level2和level3的StreamWriters,但不要关闭它们。

这可能只意味着当您尝试加载这两个级别的高分时,您会发现您之前的保存锁定了您的文件。

您应该更改Save方法以仅打开所需文件

public void save()
{
    if (diff == 1)
    {
        using(StreamWriter writerEasy = new StreamWriter("highScoresEasy.txt", true))
        {
            writerEasy.WriteLine("Time: " + hr + ":" + min + ":" + sec + " " + "Name: " + player);
        }
    }
    else if (diff == 2)
    ....
    else if (diff == 3)
    ....

我建议您在阅读方法中使用using statement,以确保在出现例外的情况下正确处理流