2014-02-28 36 views
1

在我的程序中,我正在將文本文件信息加載到富文本框中。當用戶點擊清除按鈕時,我希望文件內容爲空,並在富文本框中再次顯示。但是,當我嘗試清除錯誤時彈出該文件已被使用。試圖清除文本文件時使用的文件

我不確定這是怎麼回事,我懷疑它與關閉流讀取器或創建一個新的。無論哪種方式,我不太確定。

有沒有人有任何想法這是怎麼回事?

代碼:

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 
    } 
} 
+0

查看File類中的靜態方法 – SLaks

+0

看起來您正在使用StreamReader打開文件,然後嘗試執行一般文件寫入。如果你清除它,你不需要讀回一個空文件。 – Logarr

+1

我知道你的'.Close()'在你的文件上,技術上我認爲應該沒問題,但是最好是將'StreamReader'變量包裝在'using'語句中以確保調用'.Dispose()'。看看它是否有幫助... – LB2

回答

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);    
    } 
} 
+0

根據他在問題中做事的順序,你的第二個建議實際上是錯誤的。他想清除文件並將文本框設置爲空。 – Logarr

+0

@Logarr我不同意:他說:「當用戶點擊清除按鈕時,我希望文件內容爲空,並在富文本框中再次顯示。」它措辭不是很清楚,但聽起來他想顯示文件的內容並清除它。也許user3302467可以爲我們解決這個問題... –

+0

第三個選項效果最好。除了我切換rtxtView.Text ...去追蹤File.Write ...並且它完美運行。謝謝! – user3302467

0

封閉StreamReader申報using block {},以確保它得到處理。

試試這個:

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

OR

rtxtView.Text = File.ReadAllText(GlobalVars.strLogPath); 
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; 
} 
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; 
}