2013-03-14 71 views
1

我試圖用下面的代碼創建一個固定的長度(左對齊)批處理文件。追加拋出異常

當我使用追加它的拋出異常「是一種方法,但使用像一個類型」。

  string batFilePath = @"c:\mockforbat.bat"; 
     if (!File.Exists(batFilePath)) 
     { 
      using (FileStream fs = File.Create(batFilePath)) 
      { 
       fs.Close(); 
      } 
     } 

     //write 
     using (StreamWriter sw = new File.AppendText(batFilePath)) 
     { 
      string a = String.Format("{0,-24}{1,-5}{2,5}", "CostCenter", "CostObject", "ActivityType"); 
      sw.WriteLine(@a); 

     } 
     Process process = Process.Start(batFilePath); 
     process.WaitForExit(); 

請有人糾正我在這裏做錯了什麼?

+0

什麼是例外 – 2013-03-14 05:57:56

+1

順便說一句,這不是一個例外,但一個編譯器錯誤。 – 2013-03-14 05:58:40

回答

4

從該行

using (StreamWriter sw = new File.AppendText(batFilePath)) 

刪除該new運營商應該讀

using (StreamWriter sw = File.AppendText(batFilePath)) 
1
string batFilePath = @"c:\mockforbat.bat"; 
using(var fs = new FileStream(batFilePath , FileMode.OpenOrCreate, FileAccess.Write)) 
{ 
    using(var sw = new StreamWriter(fs)) 
    { 
     string a = String.Format("{0,-24}{1,-5}{2,5}", "CostCenter", "CostObject", "ActivityType"); 
     sw.WriteLine(a); 
    } 
}