gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 11:35:49 25 4
gpt4 key购买 nike

目前我正在将 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).

到目前为止,我已经查看了 this question 中提供的 Process.Start 方法。 ,但不要以为它可以让我经历与上面列出的过程相同的过程。

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

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

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

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

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

关于c# - 如何使用特定指南在 C# 中启动可执行文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23094232/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com