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();
Related
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()
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.
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.
I am trying to play a YouTube video in Firefox (portable), but when I run it as follows,
the UAC appears for the flash player asking if you want this program to make changes to the computer. When I run it from the commandline, it does not do this. How can I prevent this from happening? I probably should ask this as separate question, but can somebody tell why it never minimizes.
Process proc = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo(#"C:\FirefoxPortable\FirefoxPortable.exe", #"http://www.youtube.com/watch?v=1pSyYhRYeIM");
startInfo.WindowStyle = ProcessWindowStyle.Minimized;
proc.StartInfo = startInfo;
proc.Start();
Try running your application as administrator.
also, i love the video thanks for that +1
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);