简体   繁体   中英

Append throwing an exception

I was trying to create a fixed lenght(left aligned) batch file with the below code.

when i use Append it's throwing exception "is a method but used like a type".

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

Please some one correct me what i did wrong here ?

Drop the new operator from this line

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

It should read

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM