尝试清除文本文件时正在使用的文件

时间:2014-02-28 16:25:11

标签: c#

在我的程序中,我将文本文件信息加载到富文本框中。当用户单击清除按钮时,我希望文件内容为空,并在富文本框中再次显示。但是,当我尝试清除错误时,弹出该文件已被使用。

我不确定这是怎么回事,我怀疑它与关闭流阅读器或创建一个新阅读器有关。无论哪种方式,我都不太确定。

有没有人对这是怎么回事?

代码:

namespace FileLocationAutomation
{
    public partial class ViewLog : Form
    {
        public ViewLog()
        {
            InitializeComponent();
        }

        #region Variables
        string str = "";
        #endregion 

        #region Ok Button
        private void btnOK_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        #region Form Load
        private void ViewLog_Load(object sender, EventArgs e)
        {
            //catch any exception
            try
            {
                //load the log thats kept on the users machine into the rich text object 
                StreamReader read = new StreamReader(GlobalVars.strLogPath);
                str = read.ReadToEnd();
                rtxtView.Text = str;
                read.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion 

        #region Clear Log
        //clear the log file and display it on the rich text box
        private void btnClear_Click(object sender, EventArgs e)
        {
            try
            {
                StreamReader read = new StreamReader(GlobalVars.strLogPath);
                File.WriteAllText(GlobalVars.strLogPath, String.Empty);
                str = read.ReadToEnd();
                rtxtView.Text = str;
                read.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);               
            }
        }
        #endregion
    }
}

4 个答案:

答案 0 :(得分:1)

问题是您尝试写入文件以清除它,同时仍然保持打开状态。最简单的更改是将调用移至File.WriteAllText(GlobalVars.strLogPath, String.Empty);,然后关闭文件,如下所示:

 #region Clear Log
//clear the log file and display it on the rich text box
private void btnClear_Click(object sender, EventArgs e)
{
    try
    {
        StreamReader read = new StreamReader(GlobalVars.strLogPath);
        str = read.ReadToEnd();
        rtxtView.Text = str;
        read.Close();

        File.WriteAllText(GlobalVars.strLogPath, String.Empty);

    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);               
    }
}

更好的改变可能是使用静态读取方法,因为您只需一次读取整个文件就不需要使用StreamReader。

private void btnClear_Click(object sender, EventArgs e)
{
    try
    {
        rtxtView.Text = File.ReadAllText(GlobalVars.strLogPath);
        File.WriteAllText(GlobalVars.strLogPath, String.Empty);

    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);               
    }
}

答案 1 :(得分:1)

问题严格来自:

try
{
    StreamReader read = new StreamReader(GlobalVars.strLogPath);
    File.WriteAllText(GlobalVars.strLogPath, String.Empty);
    str = read.ReadToEnd();
    rtxtView.Text = str;
    read.Close();
}

让它看起来像这样:

try
{
    File.WriteAllText(GlobalVars.strLogPath, String.Empty);
    StreamReader read = new StreamReader(GlobalVars.strLogPath);
    str = read.ReadToEnd();
    rtxtView.Text = str;
    read.Close();
}

或者因为读回空白文件是没用的,所以这样做:

try
{
    File.WriteAllText(GlobalVars.strLogPath, String.Empty);
    rtxtView.Text = String.Empty;
}

答案 2 :(得分:0)

StreamReader声明附在using block {}中,以确保它被处理掉。

试试这个:

 using(StreamReader read = new StreamReader(GlobalVars.strLogPath))
 {
        str = read.ReadToEnd();
        rtxtView.Text = str;
 }

OR

rtxtView.Text = File.ReadAllText(GlobalVars.strLogPath);

答案 3 :(得分:0)

您可以更改订单。

try
{
    StreamReader read = new StreamReader(GlobalVars.strLogPath);
    File.WriteAllText(GlobalVars.strLogPath, String.Empty);
    str = read.ReadToEnd();
    // close the stream first.
    read.Close();
    rtxtView.Text = str;
}