2016-08-25 56 views
2

我創建的* .txt文件,像這樣的數據:如何更改值txt文件,並覆蓋它

deviceid,model,availability,renterID,renterName,reneterSurname,OS 
0001,iPhone_5S,false,002,John,Dowland,iOS-7.1.2 
0002,GalaxyS3,false,002,Amadeus,Mozart,Android-4.3.2 

在我的C#應用​​程序(WPF應用程序)得到這個功能對文件進行操作:現在

private void rentSaveButton_Click(object sender, RoutedEventArgs e) 
    { 
     StreamWriter sw = new StreamWriter(@"D:\deviceLib.txt", true); 

     var currentUser = _list.Where(u => u.Id == int.Parse(userIDTextBox.Text)).FirstOrDefault(); 

     var currentDevice = _list2.Where(i => i.deviceId == int.Parse(deviceIDTextBox.Text)).FirstOrDefault(); 
     if (currentDevice != null && currentUser != null) 
     { 
      currentDevice.Availability = false; 
      currentDevice.rentId = currentUser.Id; 
      currentDevice.rentName = currentUser.Name; 
      currentDevice.rentSurname = currentUser.Surname; 
      dataGridDeviceList.Items.Refresh(); 
      sw.WriteLine(); 
      sw.Close();     
      MessageBox.Show("Rent done. Thanks!"); 
      tabControl.SelectedItem = mainTab; 
     } 
     else 
     { 
      MessageBox.Show("We don't have such device. Sorry :("); 
      userIDTextBox.Clear(); 
      deviceIDTextBox.Clear(); 
     } 
    } 

,問題是,我可以在這檔節目工作中正常工作,它的變化值,可以讀取等,但是當我關閉應用程序(按X鍵)沒有發生。 TXT文件不變,也不保存任何更改。

+0

所以如果你關閉你的應用程序,你點擊保存?你有追溯這個代碼嗎? – BugFinder

+0

你嘗試設置斷點嗎?通常,前面的行是相同的,因爲新的StreamWriter(@「D:\ deviceLib.txt」,true)'告訴streamwriter在目標文件中追加新行。 – Sherlock

+0

這是一個CSV文件,[將其視爲CSV](https://www.nuget.org/packages/CsvHelper/)。 – PTwr

回答

2

你沒有寫任何文件。

sw.WriteLine(); // you need to pass a string in to this function 
+1

您還應該爲您的Streamwriter使用using語句。 – Derek

1

在我看來,你必須已經發明瞭一種理論,認爲您對currentDevice任何變動應,不知何故,影響什麼的寫入sw。你在這裏看到的是卡爾波普爾所稱的「不確定性」:你的理論預測可以觀察到的行爲;你觀察;行爲沒有發生。對此的第一個解釋是你的理論是錯誤的。

而這實際上是對你(不)看到的正確解釋。 currentDevicesw之間絕對沒有任何關係。沒有。這就像你在微波爐上按下按鈕,試圖啓動你的車。如果你不轉向另一種方法,你會步行去上班。

currentDevice.Availability = false; 
currentDevice.rentId = currentUser.Id; 
currentDevice.rentName = currentUser.Name; 
currentDevice.rentSurname = currentUser.Surname; 
dataGridDeviceList.Items.Refresh(); 
sw.WriteLine(); 
sw.Close(); 

所有你要做有改變一堆屬性的隨機對象上,刷新數據網格,然後寫換行符換行,沒有別的,到輸出流。爲什麼sw.WriteLine();,沒有參數呢?那麼,這是因爲這是設計師決定它會做的。除此之外,沒有別的東西可以這樣做,因爲你沒有給它寫任何東西。這種行爲被記錄下來,如果你花費了十秒鐘的時間,你會知道reading the documentation

如果要將非空行寫入文件,請使用許多overloads of WriteLine which are documented in the documentation之一。如果您只想寫一行部分,請使用the many overloads of Write, which are also documented之一。如果你不知道它們是什麼,也許StackOverflow上的其他人知道你的代碼部分是什麼樣子並且可以幫助你):

currentDevice.Availability = false; 
currentDevice.rentId = currentUser.Id; 
currentDevice.rentName = currentUser.Name; 
currentDevice.rentSurname = currentUser.Surname; 

// Write fields in desired order of appearance in the file. 
sw.Write(currentDevice.deviceID); 
// There are many far superior ways to write a comma to the file, 
// and I'll hear about all of them in comments, but we're keeping 
// it as simple as possible for the moment. 
sw.Write(","); 
sw.Write(currentDevice.model); 
sw.Write(","); 

// Properties you just set 
sw.Write(currentDevice.Availability); 
sw.Write(","); 
sw.Write(currentDevice.rentID); 
sw.Write(","); 
sw.Write(currentDevice.rentName); 
sw.Write(","); 
sw.Write(currentDevice.rentSurname); 
sw.Write(","); 

sw.Write(currentDevice.OS); 

// NOW write a newline. 
sw.WriteLine(); 

但這很醜。所以我們會把它推到一個方法來隱藏它。這被稱爲「重構」。

public void WriteDeviceStateToStream(StreamWriter stream, WhateverTheDeviceClassIs device) 
{ 
    // Write fields in desired order of appearance in the file. 
    stream.Write(device.deviceID); 
    // There are many far superior ways to write a comma to the file, 
    // and I'll hear about all of them in comments, but we're keeping 
    // it as simple as possible for the moment. 
    stream.Write(","); 
    stream.Write(device.model); 
    stream.Write(","); 

    // Properties you just set 
    stream.Write(device.Availability); 
    stream.Write(","); 
    stream.Write(device.rentID); 
    stream.Write(","); 
    stream.Write(device.rentName); 
    stream.Write(","); 
    stream.Write(device.rentSurname); 
    stream.Write(","); 

    stream.Write(device.OS); 

    // NOW write a newline. 
    stream.WriteLine(); 

    // DO NOT close the stream here. The stream belongs to the caller; make no 
    // assumptions about what he plans to do with it next. 
} 

...並在您的事件處理程序中調用該方法。還請注意using對於StreamWriter的聲明。正確處理它,關閉文件等等。這很重要。

private void rentSaveButton_Click(object sender, RoutedEventArgs e) 
{ 
    using (StreamWriter sw = new StreamWriter(@"D:\deviceLib.txt", true)) 
    { 
     var currentUser = _list.Where(u => u.Id == int.Parse(userIDTextBox.Text)).FirstOrDefault(); 

     var currentDevice = _list2.Where(i => i.deviceId == int.Parse(deviceIDTextBox.Text)).FirstOrDefault(); 
     if (currentDevice != null && currentUser != null) 
     { 
      currentDevice.Availability = false; 
      currentDevice.rentId = currentUser.Id; 
      currentDevice.rentName = currentUser.Name; 
      currentDevice.rentSurname = currentUser.Surname; 
      dataGridDeviceList.Items.Refresh(); 

      // Call write method 
      WriteDeviceStateToStream(sw, currentDevice); 

      // The user's going to get real tired of this messagebox real fast. 
      MessageBox.Show("Rent done. Thanks!"); 
      tabControl.SelectedItem = mainTab; 
     } 
     else 
     { 
      MessageBox.Show("We don't have such device. Sorry :("); 
      userIDTextBox.Clear(); 
      deviceIDTextBox.Clear(); 
     } 
    } 
} 
+0

我認爲重寫ToString或創建一個像ToStringForFile()這樣的新方法更有意義。對於我來說,對流進行所有這些單獨的寫入調用似乎很笨拙。當然,你也可以考慮爲什麼我們將它們作爲逗號分隔值存儲在一個文件中...... – Derek

+0

同意,一個字符串方法會更加靈活。 –

+0

字符串可以更短:'object [] elements = new [] {x.deviceid,x.model,x.availability,x.renterID,x.renterName,x.reneterSurname,x.OS}; \t sw.WriteLine(string.Join(「,」,elements));' – apocalypse