2017-02-16 109 views
1

我正在做一個項目Windows窗體的Uni分配,我想搜索一個已經創建的文本文件來匹配名字和姓氏,然後寫入一些額外的信息,如果姓名和姓氏存在。我構建了代碼並且沒有顯示任何錯誤,但是當我運行並嘗試添加信息時,我被提供了一個基本上表示下一個進程的錯誤(Streamreader編寫器無法訪問該文件,因爲它已被另一進程使用)我假設這個過程是streamreader,我試圖編碼它停止閱讀無濟於事。我在我的前3個月學習編碼,如果可能的話,我會感謝一些幫助,我已經把我的代碼片段放在下面。c#如何結束流讀取器

//check if there is a file with that name 
     if (File.Exists(sFile)) 
     { 

      using (StreamReader sr = new StreamReader(sFile)) 
      { 
       //while there is more data to read 
       while (sr.Peek() != -1) 
       { 
        //read first name and last name 
        sFirstName = sr.ReadLine(); 
        sLastName = sr.ReadLine(); 
       } 
       { 
        //does this name match? 
        if (sFirstName + sLastName == txtSearchName.Text) 

         sr.Close(); 
       } 

       //Process write to file 
        using (StreamWriter sw = new StreamWriter(sFile, true)) 

         { 
          sw.WriteLine("First Name:" + sFirstName); 
          sw.WriteLine("Last Name:" + sLastName); 
          sw.WriteLine("Gender:" + sGender); 
         } 
+0

StreamReader的右括號在哪裏。 using指令將處理流讀取器並釋放它對文件的鎖定,但是看起來好像在打開作者之後纔會關閉它? – Wheels73

+0

讀取輸入文件,寫入臨時文件,然後用臨時文件壓碎輸入文件。 –

+0

你確定你的代碼的這部分被執行了嗎?因爲它似乎在嘗試同時讀取和寫入同一個文件。 (看起來像一個可怕的想法) –

回答

1

您正在閱讀器中使用您的作者,使用相同的文件。 一個使用配置其中的對象,關閉後括號。

using(StreamReader reader = new StreamReader("foo")){ 
//... some stuff 
    using(Streamwriter writer = new StreamWriter("foo")){ 
    } 
} 

做它像這樣:

using(StreamReader reader = new StreamReader("foo")){ 
//... some stuff 
} 

using(Streamwriter writer = new StreamWriter("foo")){ 
} 
1

按照有關using語句我的意見。 重新排列到下面。我已經在本地進行了測試,並且似乎可行。

using (StreamReader sr = new StreamReader(sfile)) 
    { 
     //while there is more data to read 
     while (sr.Peek() != -1) 
     { 
      //read first name and last name 
      sFirstName = sr.ReadLine(); 
      sLastName = sr.ReadLine(); 

      //does this name match? 
      if (sFirstName + sLastName == txtSearchName.Text) 
       break; 
     } 
    } 

    using (StreamWriter sw = new StreamWriter(sfile, true)) 
    { 
     sw.WriteLine("First Name:" + sFirstName); 
     sw.WriteLine("Last Name:" + sLastName); 
     sw.WriteLine("Gender:" + sGender); 
    } 

我用一個break語句取代了sr.Close退出。關閉閱讀器會導致隨後的查看錯誤,因爲它已關閉。

此外,我注意到你沒有設置性別?除非在其他地方設置。 希望可以幫到

+0

嗨,非常感謝您的幫助,我已經刪除性別,因爲它不再需要,我仍然有一個問題,當使用此代碼提供我得到一個錯誤相關的休息; 「沒有封閉的循環可以打破或繼續」? –

+0

奇怪..仍然適用於我..你確定你沒有休息;在括號{}之外的語句? – Wheels73

0

你可以使用FileStream。它給你很多選項來處理文件:

var fileStream = new FileStream("FileName", FileMode.Open, 
FileAccess.Write, FileShare.ReadWrite); 


var fileStream = new FileStream("fileName", FileMode.Open, 
FileAccess.ReadWrite, FileShare.ReadWrite); 
0

我認爲這是你想要/需要。你不能像試圖那樣追加到文件中。相反,您需要閱讀輸入文件,並在閱讀時編寫臨時文件。而且,只要你的生產線符合你的要求,你就可以在你的修改中寫下你的生產線。

 string inputFile = "C:\\temp\\StreamWriterSample.txt"; 
     string tempFile = "C:\\temp\\StreamWriterSampleTemp.txt"; 

     using (StreamWriter sw = new StreamWriter(tempFile))//get a writer ready 
     { 
      using (StreamReader sr = new StreamReader(inputFile))//get a reader ready 
      { 
       string currentLine = string.Empty; 
       while ((currentLine = sr.ReadLine()) != null) 
       { 
        if (currentLine.Contains("Clients")) 
        { 
         sw.WriteLine(currentLine + " modified"); 
        } 
        else 
        { 
         sw.WriteLine(currentLine); 
        } 
       } 
      } 
     } 

     //now lets crush the old file with the new file 
     File.Copy(tempFile, inputFile, true); 
+0

我有一個windows窗體,它允許輸入一個客戶端的細節寫入到一個文件的6行輸入。我還有另一部分表格,其中包含用於在不同機器上執行練習分鐘的複選框,我有一個搜索框和一個按鈕,我試圖搜索當前文件中的客戶名和姓氏「輸入的姓名搜索框「如果存在,則附加新的分鐘信息。有一個單選按鈕可以選擇活動進行的那一天。所以需要將文件追加到或添加新行而不覆蓋。我需要做什麼改變? –

+0

您需要徹底重新考慮您的方法並至少使用數據庫或XML文件。對文本文件進行讀取和重寫,就好像它是一個數據庫一樣,其性能相當差。儘管如此,爲了讓我的代碼在你的場景中工作,你只需要擺弄if語句。但是你有3行修改...要麼搜索3個單獨的元素,要麼運行一次,並假設如果你找到第一個名字,那麼接下來的兩行不可避免地會是姓氏和性別。無論哪種情況,每次更新時最終都會重新編寫_database_。 –