1

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

当我使用 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(); 

请有人纠正我在这里做错了什么?

4

2 回答 2

4

new从该行删除运算符

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

它应该读

using (StreamWriter sw = File.AppendText(batFilePath))
于 2013-03-14T05:57:04.000 回答
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);
    }
}
于 2013-03-14T06:18:05.750 回答