如何在C#中启动一个具有特定指导原则的可执行程序

How to launch an executable in C# with specific guidelines

本文关键字:定指 可执行程序 原则 一个 启动      更新时间:2023-10-16

当前我正在将一个c++exe启动移植到c#。我能够通读并理解c++代码,但我很难找到等价的c#。我相信原始代码是通过使用命令提示符来启动exe的。

我认为最好显示我正在移植的代码,所以这里是:

  // This is basically running an exe to compile a file that I create
  short rtncod;
  int GPDataAge = FileAge(SelectedPath + GPDATA); //Checks age of GPDATA if it exists
  STARTUPINFO si;         // Startup information structure
  PROCESS_INFORMATION pi; // Process information structure
  memset(&si, 0, sizeof(STARTUPINFO)); // Initializes STARTUPINFO to 0
  si.cb = sizeof(STARTUPINFO); // Set the size of STARTUPINFO struct
  AnsiString CmdLine = Config->ReadString("Configuration","CRTVSM","CRTVSM.EXE . -a"); // Windows version requires path
  rtncod = (short)CreateProcess(
                  NULL,   // pointer to name of executable module
                  CmdLine.c_str(), // pointer to command line string
                  NULL,   // pointer to process security attributes
                  NULL,   // pointer to thread security attributes
                  FALSE,   // handle inheritance flag
                  CREATE_NEW_CONSOLE, // creation flags
                  NULL,   // pointer to new environment block
                  NULL,   // pointer to current directory name
                  &si,    // pointer to STARTUPINFO
                  &pi);   // pointer to PROCESS_INFORMATION
  if (!rtncod) /*If rtncod was returned successful**/ {
    int LastError = GetLastError();
    if (LastError == 87 /* Lookup 87 error **/ && AnsiString(SelectedPath + GPDATA).Length() > 99)
      ShowMessage("CRTASM could not run due to extremely long path name.  Please map or move the folder to shorten the path");
    else
      ShowMessage("Could not compile VSMInfo.dat =>" + IntToStr(LastError));
  }
  else /* If successful **/ {
    unsigned long ExitCode;
    // CartTools will 'lock up' while waiting for CRTASM
    do {
      rtncod = GetExitCodeProcess(pi.hProcess,&ExitCode);
    } while (rtncod && ExitCode == STILL_ACTIVE);
    if (rtncod == 0) {
      rtncod = GetLastError();
      ShowMessage("Could not watch CRTVSM compile VSMInfo.dat =>" + IntToStr(GetLastError()));
    }
    CloseHandle(pi.hProcess);
    CloseHandle(pi.hThread);
  }
  if (GPDataAge == FileAge(SelectedPath + GPDATA)) // date/time didn't change!
    Application->MessageBox(AnsiString("Output blocking file (" + SelectedPath + GPDATA") failed to be updated.  Check operation of CRTVSM.EXE before using "GPDATA" with SAM/CMS!").c_str(),"CRTVSM Error",MB_OK|MB_ICONHAND);

所有这些可能都不相关,您可能不知道我的个人元素来自哪里,但这没关系,因为我只关心MICROSOFT流程元素(如CreateProcessSTARTUPINFO)。

到目前为止,我已经研究了这个问题中提供的Process.Start方法,但不认为它允许我经历与上面列出的过程相同的过程。

我的问题是,我可以使用什么类或方法来自定义exe启动,其方式与上面c++代码中执行的启动方式相同

更新:目前,我的可执行文件位于我在程序解决方案中创建的文件夹中。为了启动可执行文件,我正在使用ProcessStartInfo类。

//The folder that the exe is located in is called "Executables"
ProcessStartInfo startInfo = new ProcessStartInfo("Executables\MYEXECUTABLE.EXE");
Process.Start(startInfo);

每当我运行上面的代码行时,我都会得到一个Win32Exception was unhandled,它说"系统找不到指定的文件"。

C++代码本身并没有使用命令"prompt",而是通过提供可执行文件到CreateProcess的路径来启动进程。您可以使用Process类在C#中完成同样的事情。配置Process.StartInfo并调用Start方法。

关于使用特定路径启动可执行文件:如果您没有指定完整路径,那么您将受工作目录的支配。如果exe与正在运行的可执行文件是同一目录,或者是它的子目录,那么您可以构造如下路径:

string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"ExecutablesMYEXECUTABLE.EXE");
ProcessStartInfo startInfo = new ProcessStartInfo(path);
Process.Start(startInfo);

添加到jltrem,Process.Start的一个例子是:http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo(v=vs.110).aspx

using System;
using System.Diagnostics;
using System.ComponentModel;
namespace MyProcessSample
{
class MyProcess
{
    // Opens the Internet Explorer application. 
    void OpenApplication(string myFavoritesPath)
    {
        // Start Internet Explorer. Defaults to the home page.
        Process.Start("IExplore.exe");
        // Display the contents of the favorites folder in the browser.
        Process.Start(myFavoritesPath);
    }
    // Opens urls and .html documents using Internet Explorer. 
    void OpenWithArguments()
    {
        // url's are not considered documents. They can only be opened 
        // by passing them as arguments.
        Process.Start("IExplore.exe", "www.northwindtraders.com");
        // Start a Web page using a browser associated with .html and .asp files.
        Process.Start("IExplore.exe", "C:\myPath\myFile.htm");
        Process.Start("IExplore.exe", "C:\myPath\myFile.asp");
    }
    // Uses the ProcessStartInfo class to start new processes, 
    // both in a minimized mode. 
    void OpenWithStartInfo()
    {
        ProcessStartInfo startInfo = new ProcessStartInfo("IExplore.exe");
        startInfo.WindowStyle = ProcessWindowStyle.Minimized;
        Process.Start(startInfo);
        startInfo.Arguments = "www.northwindtraders.com";
        Process.Start(startInfo);
    }
    static void Main()
    {
        // Get the path that stores favorite links. 
        string myFavoritesPath =
            Environment.GetFolderPath(Environment.SpecialFolder.Favorites);
        MyProcess myProcess = new MyProcess();
        myProcess.OpenApplication(myFavoritesPath);
        myProcess.OpenWithArguments();
        myProcess.OpenWithStartInfo(); 
    }
}
}