I'm attempting to start a Process from an ASP.NET C# application. My application communicates with this process via the process' own API (this is a third-party program). I only need to run this process under certain circumstances, so I see no need in having it running on the server constantly, hence the need to start the process from my application.
The process I'm using is set to run in a "faceless" mode via command line arguments, and so doesn't need a GUI to appear anywhere. Without giving too much away, it performs complex calculations on specific data (the data is pulled from an external source from within the process, and this data is updated constantly) and exposes the results via an API. My application communicates with this API by passing xml formatted queries and receiving similarly formatted responses.
The problem I am having is that starting the process works fine, but it runs the process as the SYSTEM user. This is a problem because the process I'm starting is registered to a specific user, and runs in a limited "trial version" mode when running as SYSTEM and the API I need does not work as desired. So instead I intend to run as the specific user the program was registered under. When I add the logon information for the correct user however, the process starts and immediately exits...
Is there some specific way I should be doing this?
Here's the code I have that is not working properly:
public static bool ProcessActive() {
return Process.GetProcesses().Any(p => p.ProcessName.Equals("Process", StringComparison.InvariantCultureIgnoreCase));
}
public static void StopProcess() {
List<Process> processExists = Process.GetProcesses().Where(p => p.ProcessName.Equals("Process", StringComparison.InvariantCultureIgnoreCase)).ToList();
foreach (Process p in processExists) {
p.Kill();
}
}
public static bool StartProcess(bool stopIfStarted = false) {
bool retVal = false;
using (System.Security.SecureString password = new System.Security.SecureString()) {
// Set up the variables required to run the process
ProcessStartInfo pInfo = new ProcessStartInfo(@"C:Program FilesProgramProgram.exe");
pInfo.Arguments = @"-arg1 -arg2";
pInfo.WorkingDirectory = @"C:Program FilesProgram";
pInfo.UserName = "username";
pInfo.Domain = "DOMAIN";
password.AppendChar('p');
/* repeat for other password chars */
pInfo.Password = password;
pInfo.UseShellExecute = false;
if (System.IO.File.Exists(pInfo.FileName)) {
//Check if the process is already running
bool processExists = ProcessActive();
if (stopIfStarted && processExists) {
StopProcess();
processExists = ProcessActive();
}
//if not, start it.
if (!processExists) {
StatusHelper.Log("Process isn't running. Starting process...");
using (Process realProcess = Process.Start(pInfo)) {
processExists = ProcessActive();
System.Threading.Thread.Sleep(1000);
processExists = ProcessActive();
if (realProcess.HasExited) {
// Always gets here
}
retVal = processExists;
}
}
}
}
return retVal;
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…