簡體   English   中英

如何使用特定准則在C#中啟動可執行文件

[英]How to launch an executable in C# with specific guidelines

目前,我正在將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方法,但是認為它不允許我經歷與上面列出的過程相同的過程。

我的問題是,我可以使用哪些類或方法來自定義與以上c ++代碼中執行的啟動等效的方式來啟動exe?

更新:目前,我的可執行文件位於我在程序解決方案中創建的文件夾內。 要啟動可執行文件,我正在使用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 ,都會得到Win32Exception was unhandled ,它說“系統找不到指定的文件”。

C ++代碼本身並不使用命令“提示”,而是通過提供可執行文件到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);

附加到jltrem上,Process.Start的示例是: http : //msdn.microsoft.com/zh-cn/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(); 
    }
}
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM