2012-07-31 106 views
2

我想刪除行末尾的空格,然後將該行寫入另一個文件。文件無法訪問,因爲它正在被另一個程序使用

但是當程序到達FileWriter然後它給我下面的錯誤,因爲它正由另一個進程

過程不能被訪問。

該代碼如下。

private void FrmCounter_Load(object sender, EventArgs e) 
{ 
     string[] filePaths = Directory.GetFiles(@"D:\abc", "*.txt", SearchOption.AllDirectories); 
     string activeDir = @"D:\dest"; 
     System.IO.StreamWriter fw; 
     string result; 

     foreach (string file in filePaths) 
     { 
      result = Path.GetFileName(file); 
      System.IO.StreamReader f = new StreamReader(file); 
      string newFileName = result; 
      // Combine the new file name with the path 
      string newPath = System.IO.Path.Combine(activeDir, newFileName); 
      File.Create(newPath); 

      fw = new StreamWriter(newPath); 

      int counter = 0; 
      int spaceAtEnd = 0; 
      string line; 

      // Read the file and display it line by line. 
      while ((line = f.ReadLine()) != null) 
      { 
       if (line.EndsWith(" ")) 
       { 
        spaceAtEnd++; 
        line = line.Substring(0, line.Length - 1); 
       } 
       fw.WriteLine(line); 
       fw.Flush(); 
       counter++; 
      } 

      MessageBox.Show("File Name : " + result); 
      MessageBox.Show("Total Space at end : " + spaceAtEnd.ToString()); 
      f.Close(); 
      fw.Close(); 
     } 
} 
+0

那麼,該文件顯然是由另一個程序打開的。如果它作爲窗口打開,也許嘗試關閉它?也許另一個程序打開就像你正在嘗試? – SimpleVar 2012-07-31 12:08:54

+0

有人猜測,因爲您將activeDir和filePaths設置爲相同的目錄,您正試圖寫入正在讀取的同一文件。 – StuartLC 2012-07-31 12:10:06

+0

那麼我試着另一個目錄..但同樣的錯誤 – 2012-07-31 12:11:28

回答

2

File.Create本身返回流。

使用該流寫入文件。您收到此錯誤的原因是因爲File.Create返回的流處於打開狀態,您正試圖再次打開該文件進行寫入。

要麼關閉,對文件的寫入流中File.Create或更好地利用返回的流或使用

Stream newFile = File.Create(newPath); 
fw = new StreamWriter(newFile); 
0

您必須先關閉您正在閱讀的任何文件,然後才能嘗試向您的案例寫信。

0

編輯:

扎法爾是正確的,但是,也許這將清除事情。

由於File.Create返回一個流..該流打開了您的目標文件。這將使事情更清晰:

File.Create(newPath).Close(); 

使用上面的行,使其工作,但是,我會建議重新寫入正確。這僅用於說明目的。 using語句像

+0

你是對的西蒙,但我編輯目標路徑,以爲我得到相同的錯誤 – 2012-07-31 12:16:02

+0

你能告訴我們你的編輯代碼? – 2012-07-31 12:16:36

+0

更改發佈後。 – 2012-07-31 12:19:14

0

寫流:

using (System.IO.StreamReader f = new StreamReader(file)) 
{ 
    //your code goes here 
} 
1

即使你解決了你最初的問題,如果你想要的一切寫入在原來的位置的新文件,您可以嘗試將所有數據讀入數組並關閉原始的StreamReader。性能注意事項:如果你的文件足夠大,這個選項不會是最好的性能。

而且你不需要File.Create作爲StreamWriter將創建一個文件,如果它不存在,或默認覆蓋它,或者如果你指定append參數爲false

result = Path.GetFileName(file); 
String[] f = File.ReadAllLines(file); // major change here... 
             // now f is an array containing all lines 
             // instead of a stream reader 

using(var fw = new StreamWriter(result, false)) 
{ 
    int counter = f.Length; // you aren't using counter anywhere, so I don't know if 
          // it is needed, but now you can just access the `Length` 
          // property of the array and get the length without a 
          // counter 

    int spaceAtEnd = 0; 

    // Read the file and display it line by line. 
    foreach (var item in f) 
    { 
     var line = item; 

     if (line.EndsWith(" ")) 
     { 
      spaceAtEnd++; 
      line = line.Substring(0, line.Length - 1); 
     } 
     fw.WriteLine(line); 
     fw.Flush(); 
    } 
} 

MessageBox.Show("File Name : " + result); 
MessageBox.Show("Total Space at end : " + spaceAtEnd.ToString()); 

此外,您不會使用此方法從行尾刪除多個空格。如果您需要這樣做,請考慮用line = line.TrimEnd(' ');替換line = line.Substring(0, line.Length - 1);

相關問題