2011-11-10 40 views
0

我正在開發一個ATM軟件系統,我在其中維護我的數據庫的文本文件。我想從文本文件中刪除記錄。我的記錄在文本文件中逐行保存。我從互聯網上找到了一個更新代碼,但是我想以這種方式刪除帳戶。請幫幫我。我想從C#文件中刪除一條記錄

StringBuilder newFile = new StringBuilder(); 
string temp = ""; 
string[] file = File.ReadAllLines(@"C:\Documents and Settings\john.grove\Desktop\1.txt"); 
foreach (string line in file) 
{ 
    if (line.Contains("string")) 
    { 
     temp = line.Replace("string", "String"); 
     newFile.Append(temp + "\r\n"); 
     continue; 
    } 
    newFile.Append(line + "\r\n"); 
} 

File.WriteAllText(@"C:\Documents and Settings\john.grove\Desktop\1.txt", newFile.ToString()); 
+3

這將是較大的文件非常低效的。這就是大多數人將數據庫存儲在數據庫中的原因。 –

+0

我的要求是文字處理。 – Smoker

+3

@Smoker是這個作業嗎? –

回答

0

你最好流這樣的:

string lines = File.ReadLines(path) 
        .Select(line => line.Replace("string", "String")); 
File.WriteAllLines(newPath, lines); 
+0

然後可能將newPath重命名爲[old] Path –