运行批处理文件并从按钮单击事件中打开exe

时间:2018-12-04 05:47:36

标签: c# windows winforms batch-file exe

我在winforms中有一个按钮,单击该按钮需要运行批处理文件并打开exe(monitor.exe)文件。批处理文件片段:-

@echo on
rem @echo $Id: runnit_navigator.bat,v X.X XXXX/XX/XX XX:XX:XX sxxxaxx Exp $

echo Running runnit_monitor.bat >%TEMP%\runnit_monitor.log
if exist "%CD%\Config.bat" (
call Config.bat >>%TEMP%\runnit_monitor.log
)
if "%1%" NEQ "" (
set SPACE_SITE=%1%
)

@set monitor_start=Y

rem if not defined SPACE_CONFIG (
rem     set SPACE_CONFIG=%TEMP%
rem )

if not exist "%PROGRAMDATA%\space\config\%SPACE_SITE% \monitor.properties" (
set SPACE_SITE=NA
)

if "%SPACE_SITE%" EQU "NA" (
call "%SPACE_HOME%\Runnit\runnit_site_monitor.bat" >>%TEMP%\runnit_monitor.log
exit
)

set SPACE_SETTINGS=%PROGRAMDATA%\space\config\%SPACE_SITE%

@set JAVA_EXE=%LoginDialogJavaHome%\bin\monitor.exe

我已经尝试过此代码,没有错误,但是exe文件根本没有打开。

Process proc = new Process();
private void btn1_Click(object sender, EventArgs e)
{
    try
    {
        string batDir = string.Format(@"C:\Program Files\Space\Runnit\");
        proc.StartInfo.WorkingDirectory = batDir;
        proc.StartInfo.FileName = "runnit_monitor.bat";
        proc.StartInfo.CreateNoWindow = false;
        proc.Start();
        proc.WaitForExit();
        proc.Close();
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.StackTrace.ToString());
    }
}

这也是上面代码的输出。弹出该窗口后,没有任何反应。但是当我设置一个断点时,我可以看到某些Process属性返回一个异常。

output

我调用过程的方式是否错误?还是对读取批处理文件后让monitor.exe运行有什么建议?

1 个答案:

答案 0 :(得分:0)

我通过引用这些链接找到了答案。

here(感谢@FredM btw),我知道问题是因为我正在从32位进程访问64位进程。我可以通过捕获Win32Exception属性来看到错误。

catch (Win32Exception w)
{
   MessageBox.Show(w.Message);
   MessageBox.Show(w.NativeErrorCode.ToString());
}

然后,我找到了一种纠正此here的方法。我不以32位应用程序的形式编译和运行程序。我只是取消勾选Project-> Build properties下的宁愿32位选项。 change project properties

最后,我进行了重建,并且运行良好! monitor.exe文件现在可以成功运行。

相关问题