0
我想通過單擊列表視圖中列出的字符串項來刪除文本文件中的一行,但出現錯誤。嘗試從ListView中的TextFile中刪除行時發生錯誤點擊 - Android C#
我正在使用的方法是將TextFile行存儲在Line<string> lines
變量中,並通過使文件爲空來覆蓋文本文件,將Line<string> lines
存儲迴文件中。
刪除行代碼:
private void lvNotes_ItemSelected(object sender, AdapterView.ItemClickEventArgs e)
{
//Where I want the Line to be deleted!
string toast = "Deleted: " + notesList.GetItemAtPosition(e.Position);
Toast.MakeText(this, toast, ToastLength.Long).Show();
string path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
string filename = Path.Combine(path, "test.txt");
List<string> lines = new List<string>();
using (StreamReader streamReader = new StreamReader(filename))
{
// 3
// Use while != null pattern for loop
string line;
while ((line = streamReader.ReadLine()) != null)
{
// 4
// Insert logic here.
// ...
// "line" is a line in the file. Add it to our List.
lines.Add(line);
}
using (StreamWriter swOvewrite = new StreamWriter(filename))
{
File.WriteAllText(filename, "");
swOvewrite.WriteLine(lines);
swOvewrite.Close();
UpdateList();
}
}
}
哦,我明白了!我會盡快嘗試。 – CodePlague
我如何閱讀,並替換/覆蓋文件? – CodePlague