嘗試進入刪除CSV文件中一些入門,如果我只知道的字符串 代碼如何刪除文件
//open File
FileStream fs = new FileStream(filePath, FileMode.Open,
FileAccess.ReadWrite);
StreamReader sr = new StreamReader(fs);
//divide file entry for separate lines
string []line = sr.ReadToEnd().Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
// find required entry and create new List without it
List<string> finalData = new List<string>();
foreach (var l in line)
{
if (!line.Contains((dateTimePicker1.Text.ToString().Trim() + ','+ eventNameDeleteTextBox.Text.ToString().Trim()+',')))
finalData.Add(l);
}
// convert to array
string toCsvoutput = string.Join(Environment.NewLine, finalData.ToArray());
// viewTextBox1.Text=toCsvoutput;
//updateFile
sr.Close();
FileStream fs1 = new FileStream(filePath, FileMode.Open,FileAccess.Write, FileShare.None);
StreamWriter sw = new StreamWriter(fs1);
sw.WriteLine(toCsvoutput);
sw.Close();
作爲結果的一部分 - 沒有heppend,在那裏我錯了嗎?
是看來你曲解我的【答案】(http://stackoverflow.com/a/18422577/ 2530848)。在我的答案'行'是迭代器變量。 'string []'被命名爲'lines',它也給出了適當的含義。 –
是的,我描述了我的答案,因爲我不能描述評論中的所有代碼 - 在新問題中更好的視覺,對不起,如果我冒犯了你,但實際上你的幫助非常有幫助 - 我只是試着學習它 - 並需要很多幫助 – gbk
不。沒關係。我以爲我完全回答你的問題。正如我上面指出的那樣,沒有任何問題開始使用「標識符(變量名稱)」。這是避免像這樣不必要的混淆的最佳做法。:)很高興幫助 –