简体   繁体   English

在Windows XP虚拟机上使用process.start的C#Windows Forms应用程序

[英]C# windows forms app using process.start on Windows XP Virtual machine

Here is the situation. 这是情况。 We have a very old COBOL exe that will only run on Windows XP. 我们有一个非常老的COBOL exe,只能在Windows XP上运行。 In order to run this exe we have set up a virtual XP machine. 为了运行此exe,我们已经设置了虚拟XP计算机。 Each week the user runs a series of BAT files. 用户每周都会运行一系列BAT文件。

I'm trying to write a C# Windows app that will run on the VM and then execute the COBOL exe. 我正在尝试编写将在VM上运行的C#Windows应用程序,然后执行COBOL exe。 The plan is to use Process.Start to call the EXE. 计划是使用Process.Start来调用EXE。 Everything works fine IF MY WINDOWS C# EXE runs in the same directory as the COBOL exe. 如果MY WINDOWS C#EXE与COBOL exe在同一目录中运行,则一切正常。

When I move the C# exe out of the folder, I get a bad return code and the cobol does not get executed. 当我将C#exe移出文件夹时,返回错误代码,并且未执行cobol。

Without going into a lot of detail, I want to be able to run the COBOL from outside of the folder. 无需赘述,我希望能够从文件夹外部运行COBOL。 The reason is that we have several regional offices and each one has its own folder and its own copy of the cobol exe inside of each folder. 原因是我们有几个地区办事处,每个办事处都有自己的文件夹,并且每个文件夹中都有自己的cobol exe副本。 The goal here is to let the user pick each regional office and then the C# program will execute each program in each directory. 这里的目标是让用户选择每个区域办事处,然后C#程序将在每个目录中执行每个程序。

In summary. 综上所述。 If I run C# inside the folder where the COBOL exe lives, IT WORKS. 如果我在COBOL exe所在的文件夹中运行C#,则可以正常工作。 BUT if I move C# outside of that folder it DOES NOT 但是,如果我将C#移到该文件夹​​之外,则不会

I hope all this makes sense. 我希望所有这些都是有道理的。 Here are some additional details: 以下是一些其他详细信息:

  1. Created in Visual Studio 2012 as a C# Windows Application 在Visual Studio 2012中作为C#Windows应用程序创建
  2. Platform target x86 (I've also used Any CPU) 平台目标x86(我也使用过任何CPU)
  3. Target framework .NET Framework 4 目标框架.NET Framework 4
  4. COBOL is Microfocus from 1997 (yes I know. We need to get rid of it) COBOL是1997年的Microfocus(是的,我知道。我们需要摆脱它)
  5. VM is Windows XP VM是Windows XP
  6. If I change the exe to Notepad it runs fine 如果我将exe更改为记事本, 则可以正常运行
  7. If I do start run from windows and paste this: K:\\AMSapp\\MYTESTFOLDER\\AMSBLD.exe it runs fine . 如果我确实从Windows开始运行并将其粘贴:K:\\ AMSapp \\ MYTESTFOLDER \\ AMSBLD.exe, 它将正常运行

  8. C# code for button click event (note: if I change the filename to this K:\\AMSapp\\MYTESTFOLDER\\AMSBLD.exe i get the same results. It does not run ) Also the message box is showing 255 for a return code when it does not work. 按钮单击事件的C#代码(注意:如果我将文件名更改为此K:\\ AMSapp \\ MYTESTFOLDER \\ AMSBLD.exe,我将得到相同的结果。 它不会运行 )此外,消息框在返回时显示255,表示返回代码不起作用。 If the C# program is moved inside of the cobol folder it returns a zero. 如果将C#程序移至cobol文件夹内,则它将返回零。

     private void button1_Click(object sender, EventArgs e) { try { int exitCode; Process process = new Process(); process.StartInfo.FileName = @"\\\\SERVER23021\\PRODDATA\\AMSapp\\MYTESTFOLDER\\AMSBLD.exe"; process.StartInfo.ErrorDialog = true; process.Start(); process.WaitForExit(1000 * 60 * 5); // Wait up to five minutes. exitCode = process.ExitCode; MessageBox.Show("My exit code = " + exitCode.ToString()); MessageBox.Show("my path and file name: " + process.StartInfo.FileName); } catch (Exception ex) { MessageBox.Show(ex.Message); } } 

尝试将进程工作目录设置为cobol exe文件的相同路径。

process.StartInfo.WorkingDirectory = System.IO.Path.GetFullPath(process.StartInfo.FileName);

I think, you should use ProcessStartInfo.Domain to set the domain and after that path to exe within ProcessStartInfo.FileName . 我想,你应该使用ProcessStartInfo.Domain设置域和范围内的路径为EXE后ProcessStartInfo.FileName Instance of ProcessStartInfo pass into Process.Start method ProcessStartInfo实例传递给Process.Start方法

ProcessStartInfo psi = new ProcessStartInfo();
psi.Domain = @"SERVER23021";
psi.FileName = @"\PRODDATA\AMSapp\MYTESTFOLDER\AMSBLD.exe";

Process.Start(psi);

If you need to personate your calling you can set Username and Password . 如果需要模拟通话,可以设置UsernamePassword But you must remember that you need to provide them as a pair. 但是您必须记住,您需要将它们作为一对提供。 UseShellExecute must be set to false and WorkingDirectory must be also provided. UseShellExecute必须设置为false并且还必须提供WorkingDirectory More: https://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.password(v=vs.110).aspx 更多: https : //msdn.microsoft.com/zh-cn/library/system.diagnostics.processstartinfo.password(v=vs.110).aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM