interfering with mongo.exe with console - c#

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()

Related

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();

Add more than one arguments when launch a process c#

Hy,
I need to launch a vnc viewer in a winform (ultravnc in my case) and I need to send two parameters to see the remote desktop and after several issues, I can't find any solutions.
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.EnableRaisingEvents = false;
proc.StartInfo.FileName = "C:/Program Files/uvnc bvba/ultravnc/vncviewer.exe";
proc.StartInfo.Arguments = host;
proc.Start();
the first argument "host" is the ip of the pc and that just work.
After that, ultravnc ask me the password for the remote desktop connection.
And this is where I can't find any solutions :
1)I try to add a second arguments :
proc.StartInfo.Arguments = mdpVNC;
But VNC take this as a replacement of the "host" variable.
2)I try to use the SendKeys class but it doesn't work
3)I try the property "PasswordInClearText" but that doesn't work either.
I try several things and I don't want to use an external package (like vncSharp or other, because these solutions don't suit me)
I need help plz.
Thanks in advance.
StartInfo.Arguments is a string, and you put in there the arguments. Make the string that has the arguments as you would write them in the command line. For example:
startInfo.Arguments = "host -dsmplugin msrc4plugin.dsm";
That is for the first example in UltraVNC Viewer Commandline Parameters
.
Or whatever you need.

docker-machine commands from C#

Is there another way to execute docker-machine commands from C#. Currently the way I'm doing it is:
Starting a Process
The Process starts "C:\Program Files\Git\bin\bash.exe"
Pass the docker-machine inspect default command as a ProcessStartInfo argument
This is the code that I'm currently using and at the moment still don't have no idea how to retrieve the return JSON from the docker-machine inspect default command but I'm guessing that I it can be retrieved from the process.StandardOutput stream.
ProcessStartInfo psi = new ProcessStartInfo();
psi.WindowStyle = ProcessWindowStyle.Normal;
psi.FileName = #"C:\Program Files\Git\bin\bash.exe";
psi.WorkingDirectory = #"C:\Program Files\Docker Toolbox";
psi.Arguments = #"--login -i ""C:\Program Files\Docker Toolbox\start.sh"" docker-machine inspect default";
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
Process process = Process.Start(psi);
As you can see this is quite tedious to do, for instance if I have to do another couple of docker-machine commands.
I'm also looking at the Docker Remote API but I don't seem to see an endpoint that interacts with the Docker Machine.
UPDATE:
One problem that I have with executing commands this way is that sometimes the commands are not evaluated properly. For instance eval $(docker-machine env default) works if executed on the Docker Toolbox but not on the code.

How to run chkdsk on drive without command prompt in c#

I searched on internet and found many possible ways of running chkdsk utility in c# using cmd.exe as file name and passing command as /c chkdsk drive_letter:/f /x
But I want to run chkdsk utility on drive ,without involvement of cmd.exe i.e; Purely using internal features of c#.
Your suggestions are greatly appreciated.
Chkdsk is a console application. So how do you wanna start a console application without the console? The only thing you can do is hiding the window from the user.
As you for sure already noticed: MSDN - Chkdsk
According the console-window:
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
This together should bring you to your goal.

Write code for execution time on C# Command Line Application

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);

Categories

Resources