追加抛出异常

本文关键字:抛出异常 追加 | 更新日期: 2023-09-27 18:03:53

我试图用下面的代码创建一个固定长度(左对齐)的批处理文件。

当我使用Append时,它抛出异常"是一个方法,但像一个类型一样使用"。

            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(); 

谁来指正一下我做错了什么

追加抛出异常

从本行删除new操作符

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

应该是

using (StreamWriter sw = File.AppendText(batFilePath))
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);
    }
}