2013-09-23 74 views
5

我在玩C#並遇到了一個問題。 當我嘗試創建一個新文件時,程序中斷並且表示該文件正在被另一個進程使用。這可能是我忽視的一些愚蠢的東西,但我無法弄清楚它!C#異常。文件正在被另一個進程使用

這裏是我的代碼:

using System; 
using System.IO; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace myNameSpace 
{ 
    public partial class MainWindow : Form 
    { 
     public static string folderPath = @"fullpath"; 
     public MainWindow() 
     { 
      InitializeComponent(); 
      StoredTb.Text = folderPath; 
      String[] files = Directory.GetFiles(folderPath); 
      foreach (string file in files) 
       myDropDown.Items.Add(Path.GetFileNameWithoutExtension(file)); 
     } 

     private void myDropDown_SelectedIndexChanged(object sender, EventArgs e) 
     { 
      this.BeginInvoke(new MethodInvoker(myDropDown_invokedMethod)); 
     } 

     private void myDropDown_invokedMethod() 
     { 
      string fullpath = MainWindow.folderPath + myDropDown.SelectedText + ".txt"; 
      StreamReader sr = new StreamReader(fullpath); 
      NameTb.Text = myDropDown.SelectedText; 
      DescriptionTb.Text = sr.ReadToEnd(); 
      sr.Close(); 
     } 

     private void SaveBtn_Click(object sender, EventArgs e) 
     { 
      File.Create(NameTb.Text + ".txt"); 
      TextWriter tw = new StreamWriter(NameTb.Text + ".txt"); /* this is where the problem occurs */ 
      tw.WriteLine("The very first line!"); 
      tw.Close(); 
     } 
    } 
} 

對不起,長的代碼片段,但因爲我不知道哪裏出了問題來源於我必須包括幾乎一切。

回答

9

您的問題是File.Create將打開一個stream讓你做你喜歡的文件是什麼:

http://msdn.microsoft.com/en-us/library/d62kzs03.aspx

提供對路徑中指定的文件的讀/寫訪問權限的FileStream。

因此,從技術上講,它已被使用。

只需刪除File.CreateStreamWriter將處理創建該文件,如果它不存在。

此外,投資using構造來正確處理您的文件連接。將來有助於避免這種情況。

+0

謝謝!有效。我會在6分鐘內接受(所以不會讓我接受) –

1

使用

myDropDown_invokedMethod(); 

,而不是

this.BeginInvoke(new MethodInvoker(myDropDown_invokedMethod)); 
+0

謝謝!這不是造成這種例外的原因,但我會確保改變它。 –

0

那是因爲File.Create(NameTb.Text +「.txt」);保持文件打開:

嘗試用這個代替:

private void SaveBtn_Click(object sender, EventArgs e) 
    { 
     using(Stream stream = File.Create(NameTb.Text + ".txt")) 
     { 
      TextWriter tw = new StreamWriter(stream); /* this is where the problem was */ 
      tw.WriteLine("The very first line!"); 
      tw.Close(); 
     } 
    } 

這港島線強制File.Create當方法退出到處置。該配置將關閉文件句柄(win32非託管句柄)。否則,文件將被垃圾收集器關閉,但你永遠不知道什麼時候。

1

File.Create返回保存文件的FileStream對象。您應該使用此對象進行進一步的任務。

var fileStream = File.Create(NameTb.Text + ".txt"); 
//... do all the writing using fileStream 
fileStream.Close(); 

,或者你可以做到這

var fs = File.Create(NameTb.Text + ".txt"); 
fs.Close(); 
TextWriter tw = new StreamWriter(NameTb.Text + ".txt"); 
tw.WriteLine("The very first line!"); 
tw.Close(); 
1

您的問題似乎是在SaveBtn_Click事件,您使用的是目標文件兩次寫入:

File.Create(NameTb.Text + ".txt"); 
TextWriter tw = new StreamWriter(NameTb.Text + ".txt"); 

刪除此行:

File.Create(NameTb.Text + ".txt"); 
1

按照MSDN File.Create Method (String)使用FileStream,在您的情況下,它沒有關閉。使用這樣的事情:

FileStream fs = new FileStream(NameTb.Text + ".txt"); 
File.Create(fs); 
fs.Close(); 

或@Muctadir第納爾

var fileStream = File.Create(NameTb.Text + ".txt"); 
//... do all the writing using fileStream 
fileStream.Close(); 
相關問題