public static CmdResult Run(string fileName, string args) { Process p = new Process(); p.StartInfo.FileName = fileName; p.StartInfo.Arguments = args; p.StartInfo.UseShellExecute = false; p.StartInfo.CreateNoWindow = true; p.StartInfo.RedirectStandardInput = true; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.RedirectStandardError = true; CmdResult cmdResult = new CmdResult("", ""); try { p.Start(); cmdResult.Set(p.StandardOutput.ReadToEnd(), p.StandardError.ReadToEnd()); p.WaitForExit(); } catch (Exception e) { Console.WriteLine("运行 " + fileName + " 出现错误\n" + e.StackTrace); } return(cmdResult); }
public static CmdResult CmdRun(string command) { command += "&exit"; Process p = new Process(); p.StartInfo.FileName = "cmd.exe"; p.StartInfo.UseShellExecute = false; //是否使用操作系统shell启动 p.StartInfo.RedirectStandardInput = true; //接受来自调用程序的输入信息 p.StartInfo.RedirectStandardOutput = true; //由调用程序获取输出信息 p.StartInfo.RedirectStandardError = true; //重定向标准错误输出 p.StartInfo.CreateNoWindow = true; //不显示程序窗口 p.Start(); //启动程序 //向cmd窗口发送输入信息 p.StandardInput.AutoFlush = true; string[] commands = command.Split('\n'); foreach (string line in commands) { p.StandardInput.WriteLine(command); } string result = p.StandardOutput.ReadToEnd(); string error = p.StandardError.ReadToEnd(); p.WaitForExit(); p.Close(); CmdResult cmdResult = new CmdResult(result, error); return(cmdResult); }
public static bool Install() { string path = EnvChecker.GetVscPath(); CmdResult result = CmdRunner.CmdRun( path.Substring(0, 2) + "\ncd " + path + "bin" + "\ncode --install-extension vscjava.vscode-java-pack"); if (result.result.Contains("is already installed") || result.result.Contains("was successfully installed")) { return(true); } else { System.Windows.Forms.MessageBox.Show("无法安装VScode拓展:" + Environment.NewLine + result.result + Environment.NewLine + "该过程并不影响Java SE环境配置" + Environment.NewLine + "您可以稍后手动安装 vscjava.vscode-java-pack 插件" , "哎呦被玩坏了", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Exclamation); return(false); } }
public static bool CheckJavaSE() { CmdResult result = CmdRunner.CmdRun("javac -version"); string output = result.result.Substring(result.result.IndexOf("&exit") + 5); if (output.Contains(".")) { return(true); } return(false); }
public static bool CheckVscExtension() { string path = GetVscPath(); CmdResult result = CmdRunner.CmdRun( path.Substring(0, 2) + "\ncd " + path + "bin" + "\ncode --list-extensions"); if (result.result.Contains("vscjava.vscode-java-pack")) { return(true); } else { return(false); } }
public static bool CheckVscode() { string path = GetVscPath(); if (path == string.Empty) { return(false); } CmdResult result = CmdRunner.CmdRun( path.Substring(0, 2) + "\ncd " + path + "bin" + "\ncode -version"); string output = result.result.Substring(result.result.IndexOf("&exit") + 5); if (output.Contains(".")) { return(true); } return(false); }