C#修改系统环境变量,调用批处理bat

本文介绍了如何使用C#设置环境变量以及如何通过C#程序调用批处理文件。首先展示了如何检查并添加路径到环境变量中,然后演示了如何启动一个批处理文件并等待其完成执行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、设置环境变量

  1. public void SetPath(string pathValue)
  2.         {
  3.             string pathlist;
  4.             pathlist = Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Machine);
  5.             string[] list = pathlist.Split(';');
  6.             bool isPathExist = false;
  7.             foreach (string item in list)
  8.             {
  9.                 if (item == pathValue)
  10.                     isPathExist = true;
  11.             }
  12.             if (!isPathExist)
  13.             {
  14.                 Environment.SetEnvironmentVariable("PATH", pathlist + ";" + pathValue, EnvironmentVariableTarget.Machine);
  15.             }
  16.         }


二、程序调用批处理

    1.                 Process proc = new Process();
    2.                 proc.StartInfo.WorkingDirectory = Application.StartupPath;
    3.                 proc.StartInfo.FileName = "service install.bat";
    4.                 proc.StartInfo.Arguments = String.Format("10");
    5.                 proc.StartInfo.CreateNoWindow = true;
    6.                 proc.Start();
    7.                 proc.WaitForExit();
    8.                 MessageBox.Show("Create Success!");

转载于:https://www.cnblogs.com/lsgsanxiao/p/6903955.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值