2013-03-14 76 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); 
    } 
}