如何使用特定指南在C#中启动可执行文件

时间:2014-04-15 20:48:54

标签: c# c++ wpf exe system.diagnostics

目前我正在将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流程元素(例如CreateProcess和{{1} })。

到目前为止,我已经查看了this question中提供的STARTUPINFO方法,但不认为它允许我通过与上面列出的相同的过程。

我的问题是,我可以使用哪些类或方法来自定义我的exe启动,其方式与上述c ++代码中执行的启动等效?

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

Process.Start

每当我运行上面的代码行时,我得到一个//The folder that the exe is located in is called "Executables" ProcessStartInfo startInfo = new ProcessStartInfo("Executables\\MYEXECUTABLE.EXE"); Process.Start(startInfo); ,它说“系统找不到指定的文件”。

2 个答案:

答案 0 :(得分:1)

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

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

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

答案 1 :(得分:0)

添加到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(); 
    }
}
}