如何在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流程元素(如CreateProcess
和STARTUPINFO
)。
到目前为止,我已经研究了这个问题中提供的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();
}
}
}
- 如何用共享库替换可执行C++程序的功能?
- 如果我在 Linux 上更改C++动态共享库,而我的可执行程序在其上使用,会发生什么
- 调试和发布库及其可执行程序
- 使用 RS232 电缆将可执行程序发送到硬件(微处理器)
- 调用Android内部的C++可执行程序
- c++可执行程序的分布式计算
- 在 Ubuntu 中创建一个文件作为 C 中的可执行程序
- 源代码构建中的 SCons 仅适用于可执行程序,不适用于目标文件
- 当我们不给 C 可执行程序参数时如何处理异常
- C++11与OpenMP结合使用,可执行程序速度较慢
- 从QtCreator导出可执行程序
- c++虚拟函数和可执行程序
- 启动使用附加库的可执行程序
- 英特尔至强Phi -运行多个单线程可执行程序
- 如何在c++中创建独立的可执行程序
- valgrind/callgrind可以在发布的可执行c++程序上工作吗?
- 使用nvcc的可执行程序比使用gcc/g++和OpenCL的可执行文件大
- C++的可执行程序场景(BDD)
- 如何在C#中启动一个具有特定指导原则的可执行程序
- Readelf报告程序是一个共享库,而不是可执行程序