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

一、设置环境变量

  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!");
posted @ 2017-05-25 15:13  三小  阅读(1251)  评论(0编辑  收藏  举报