C#运行批处理文件会出现一些错误

时间:2018-04-08 08:11:33

标签: batch-file

我正在尝试为Minecraft制作服务器程序。我用Visual Studio创建了一个表单,我添加了一个名为" Start Server"的按钮。

"启动服务器"按钮代码:

Process.Start("C:\\Users\\w7\\Desktop\\UxMux Server\\start.bat");

此代码可以很好地打开批处理文件。我可以启动我的服务器了。但是,在批处理文件打开时我得到了这个错误: Getting error from batch file

但如果我手动启动批处理文件,我就不会收到任何错误。但如果我启动批处理文件,我会收到错误。

我的" start.bat"批处理文件代码:

@echo off
title Minecraft Plugin Test Server
mode 1000
java -Xms328M -Xmx328M -jar spigot.jar nogui
PAUSE

节目图片(注:土耳其按钮,Beacuse I Am Turkish) The program picture

我无法找到该系统的任何解决方案。

1 个答案:

答案 0 :(得分:0)

基本上,当您使用 Process.Start(),并将 UseShellExecute 设置为 false 时,它会调用Windows CreateProcess( ) API函数。该函数使用当前进程的PATH环境变量(您的.NET应用程序,而不是您要启动的那个。当设置为 true 时,使用 ShellExecute 函数

尝试,

string path = System.Environment.GetEnvironmentVariable("path");
path += @";c:\your_nedded_path;";
System.Environment.SetEnvironmentVariable("path", path);
Process.Start("C:\\Users\\w7\\Desktop\\UxMux Server\\start.bat");

或者使用 ProcessStartInfo 结构

ProcessStartInfo procStartInfo = new ProcessStartInfo()
{
  WorkingDirectory = "C:\\Users\\w7\\Desktop\\UxMux Server",
  UseShellExecute = true,
  FileName  = "cmd",
  Arguments = "/c start.bat" // + parameters
};
Process p = new Process();
p.StartInfo = procStartInfo;
p.Start();