2013-04-26 72 views
-2

我想更新在文本文件中的某一部分/線路..如何更新文本文件中的一行?

我有這樣的代碼,但它不工作:

public static void UpdatingMethod() 
{ 
    string text = File.ReadAllText("fileone.txt"); 
    text = text.Replace("old value", "new stuff"); 
    File.WriteAllText("fileone.txt", text); 
} 

public static void WritingMethod() 
{ 
    int count = 0; 
    while (count < 10) 
    { 
     Console.Write(" Enter your Name: "); 
     Name = Console.ReadLine(); 

     Console.Write(" Enter your ID: "); 
     ID = int.Parse(Console.ReadLine()); 

     Console.Write(" Enter your Age: "); 
     Age = int.Parse(Console.ReadLine()); 

     Console.Write(" Enter your E-mail: "); 
     Email = Console.ReadLine(); 

     StreamWriter Sw = new StreamWriter("fileone.txt", true); 
     string output = string.Format("Thank you for registration! Your Submitted information are:" + Environment.NewLine + "Name: {0}" 
     + Environment.NewLine + "ID: {1}" + Environment.NewLine + "Age: {2}" + Environment.NewLine + "E-mail: {3}", Name, ID, Age, Email);   
     Console.WriteLine(output);  
     Sw.WriteLine(output + Environment.NewLine); 
     Console.ReadLine(); 

     Sw.Close(); 
     count++; 
    } 
} 
+0

怎麼錯了? – 2013-04-26 03:13:07

+0

當我調用方法時,cmd會自動打開和關閉而不顯示任何東西! – 2013-04-26 03:14:26

+0

你期望顯示什麼?你不會在這裏寫任何東西給控制檯。 – 2013-04-26 03:15:12

回答

2

你需要有作爲主要方法一個入口點給你的應用程序。

class TestClass 
{ 
    static void Main(string[] args) 
    { 
     // not really good names for what it's worth 
     WritingMethod(); 
     UpdatingMethod(); 
    } 
} 

MSDN Main() & Command Line arguments

相關問題