Write code for execution time on C# Command Line Application - c#

I want to have my program execute a bunch of commands on load-time and this is in C# btw, but it's a console program, how can I do that?

If you are trying to execute external applications from within your C# console application, see the ProcessStartInfo and Process class.
Example:
Process.Start("IExplore.exe", "www.google.com");
// -- OR --
ProcessStartInfo startInfo = new ProcessStartInfo("IExplore.exe");
startInfo.WindowStyle = ProcessWindowStyle.Maximized;
startInfo.Arguments = "www.google.com";
Process.Start(startInfo);

Related

C# Windows Service with Registry Access and rundll32.exe

I have a C# Windows Service running under the LocalSystem account.
I need access to the Windows registry in this service. I assume that this is only possible when running as LocalSystem? Or can I install the service as User context and have access to HKEY_CURRENT_USER?
The systems where the service is used is normally just used by one user. So there will be just "one" HKEY_CURRENT_USER. Another option is, to setup Remote Apps not for one user, but for all users. But I have no idea how to do that. Currently I know only the way listed below by running rundll32.exe.
As a second requirement I need to execute rundll32.exe to create RemoteApp Connection.
The following Code is not working properly:
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;
startInfo.FileName = "cmd.exe";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = "/C rundll32.exe tsworkspace,WorkspaceSilentSetup C:\\RemoteConfig.wcx";
using (Process process = Process.Start(startInfo))
{
process.WaitForExit();
}
What must the Service be like to access HKEY_CURRENT_USER?
How can I run rundll32.exe? just running it directly caused some trouble when testing it in a console application. But with help of cmd.exe it worked.

interfering with mongo.exe with console

ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "cmd.exe";
psi.Arguments = #"/C cd C:\Program Files\MongoDB\Server\4.2\bin & dir & pause";
using (Process p = Process.Start(psi))
{
p.WaitForExit();
}
With this code, I first open cmd and then run mongo.exe. But after switching to mongo shell, I can't send any commands (with Code).
My goal is to automate Mongo ReplicaSet. For this, I need to get input and output by interfering with mongoshell.
Thank you in advance for your help.
There is an easier way of doing this.
Start the MongoDB instance and put it in background.
Open a new mongo shell and run your commands.
When you like to use the mongo shell then you should use native JavaScript commands, i.e. start shell with javascript file name (see Core Options)
If you prefer to do all in C# then you should use the MongoDB C#/.NET Driver
As last command in your shell script put db.getSiblingDB("admin").shutdownServer() - by this the first window will terminate and you don't need any WaitForExit()

Running a .ps1 PowerShell Script From C#

Attempting to start a .ps1 script by clicking a button in my C# application. My tool will earlier have had you save the file & if PowerShell is selected it will write a text file that looks like this:
PS
C:\Documents\Test\Test.ps1\
The scripts will have already been created & most will accept user input inside the PowerShell script itself. I've tested a few things the following launches a PowerShell window, then flies through the script I am testing with, and finally closes. However if I were to launch the PS script traditionally it would prompt the user for input multiple times, then perform its actions, and finally close.
System.Diagnostics.Process.Start(#"powershell.exe", lines[i])
However in order for this to work I had to edit the .txt file to the following, and this is useless because it is just spitting out the text of the script itself
PS
${C:\Documents\Test\Test.ps1\}
Then the following just does not work no matter how I format the .txt file
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = #"powershell.exe";
startInfo.Arguments = lines[i];
startInfo.CreateNoWindow = true;
Process process = new Process();
process.StartInfo = startInfo;
process.Start();

What is reliable way to start external program for c# application

From my C# application I want to start another application that requires admin privilleges and has manifest that ensures it.
My part of code:
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(#"MyLauncher.exe");
startInfo.Arguments = "/a";
startInfo.UseShellExecute = true;
startInfo.Verb = "runas";
System.Diagnostics.Process p = System.Diagnostics.Process.Start(startInfo);
Thread.Sleep(5000);
Current.Shutdown();
Unfortunatelly sometimes application start smoothly but from time to time only UAC dialog appears but the application does not start.
I tried different startup settings but no luck.

How can I execute an Asynchronous command-line process from a windows form in C#?

I have a Windows form application that currently executes NANT in a console application to run our software build process. I'm looking for an example on how to execute console process (with command-line) asynchronously. I found a sample project on Asynchronous Design Patterns on the CodeProject website but it doesn't mention anything about console applications unless I'm not not understanding the concept they show.
I have a class that has a method called run with executes a console command (NANT.EXE) by shelling out.
ProcessStartInfo si = new ProcessStartInfo();
si.Arguments = CreateNAntCommandLine();
si.FileName = m_Settings.NAntExe;
si.WorkingDirectory = m_Settings.CurDir;
Process proc = new Process();
proc.StartInfo = si;
bool bProcStarted = proc.Start();
Jim

Categories

Resources