C#修改系统环境变量,调用批处理bat
一、设置环境变量
- public void SetPath(string pathValue)
- {
- string pathlist;
- pathlist = Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Machine);
- string[] list = pathlist.Split(';');
- bool isPathExist = false;
- foreach (string item in list)
- {
- if (item == pathValue)
- isPathExist = true;
- }
- if (!isPathExist)
- {
- Environment.SetEnvironmentVariable("PATH", pathlist + ";" + pathValue, EnvironmentVariableTarget.Machine);
- }
- }
二、程序调用批处理
- Process proc = new Process();
- proc.StartInfo.WorkingDirectory = Application.StartupPath;
- proc.StartInfo.FileName = "service install.bat";
- proc.StartInfo.Arguments = String.Format("10");
- proc.StartInfo.CreateNoWindow = true;
- proc.Start();
- proc.WaitForExit();
- MessageBox.Show("Create Success!");
作者:三小
声明:原创博客请在转载时保留原文链接或者在文章开头加上本人博客地址,如发现错误,欢迎批评指正。凡是转载于本人的文章,不能设置打赏功能,如有特殊需求请与本人联系!