• C#实现bat文件调用


    分成2个步骤,首先生成一个bat文件,然后调用批处理文件

    1.生成.bat文件

    入参为文件的内容,filePath为绝对路径,且需要扩展名(这个方法不局限于生成.bat文件,也可以生成其他扩展名文件)

     public void writeBATFile(string fileContent)
            {string filePath = "D:\test\testChange.bat";
                if (!File.Exists(filePath))
                {
                    FileStream fs1 = new FileStream(filePath, FileMode.Create, FileAccess.Write);//创建写入文件
                    StreamWriter sw = new StreamWriter(fs1);
                    sw.WriteLine(fileContent);//开始写入值
                    sw.Close();
                    fs1.Close();
                }
                else
                {
                    FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Write);
                    StreamWriter sr = new StreamWriter(fs);
                    sr.WriteLine(fileContent);//开始写入值
                    sr.Close();
                    fs.Close();
                }
            }

    2.调用.bat文件

    这里需要使用一个命名空间

    using System.Diagnostics;

    调用文件代码为

      Process proc = null;
                try
                {
                    string targetDir = string.Format(@"D:BizMap");//this is where testChange.bat lies
                    proc = new Process();
                    proc.StartInfo.WorkingDirectory = targetDir;
                    proc.StartInfo.FileName = "testChange.bat";
                    proc.StartInfo.Arguments = string.Format("10");//this is argument
                    //proc.StartInfo.CreateNoWindow = true;
                    //proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;//这里设置DOS窗口不显示,经实践可行
                    proc.Start();
                    proc.WaitForExit();
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Exception Occurred :{0},{1}", ex.Message, ex.StackTrace.ToString());
                }
  • 相关阅读:
    Apache配置
    linux centos使用xrdp远程界面登陆
    实现台式机redhat6.4无线网卡上网RTL8188CUS
    linux-redhat6.4驱动无线网卡rtl8188eu
    linux开机自动连接无线网络
    javascript for循环 日期 select
    amazeui折叠面板智能化展开
    PHP Ueditor 富文本编辑器
    vue.js 使用小结
    php mysqli mysqli_query() mysqli_real_query()
  • 原文地址:https://www.cnblogs.com/wxwx/p/5821059.html
Copyright © 2020-2023  润新知