C# 读写bat文件

时间:2021-08-19 09:59:03

读:

var batFile = "D:\\test.bat";
if (File.Exists(batFile))
{
    using (var sr = new StreamReader(batFile))
    {
        var content = sr.ReadToEnd();
    }
}

  

写:

var batFile = "D:\\test.bat";
using (var fs = new FileStream(batFile, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
    using (var sw = new StreamWriter(fs))
    {
        sw.WriteLine("Hello World");
    }
}