C# - Process object not running cmd command - c#

I'm using WinAppDriver in order to run some test cases on Excel. I'm trying to start the server through the code so that I don't have to manually do it in command line. I have the following code-
public static void StartWinAppServer(int port) {
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Normal;
startInfo.FileName = "cmd.exe";
startInfo.WorkingDirectory = #"C:\Program Files (x86)\Windows Application Driver\";
startInfo.Arguments = $"WinAppDriver {port}";
process.StartInfo = startInfo;
process.Start();
}
Which is called like this-
public static WindowsDriver<WindowsElement> GetWindowsAppDriver (AppName appName) {
string appID = string.Empty;
StartWinAppServer(4723);
switch(appName) {
case AppName.Excel:
appID = #"C:\Program Files\Microsoft Office\root\Office16\Excel.exe";
break;
}
DesiredCapabilities appCapabilities = new DesiredCapabilities();
appCapabilities.SetCapability("app", appID);
return new WindowsDriver<WindowsElement>(new Uri("http://127.0.0.1:4723"), appCapabilities);
}
This code opens up the CMD but isn't running it. Am I missing something here? I thought the arguments property would've done the trick.

Try adding the /K or /C flag to startInfo.Arguments. This tells cmd.exe to run the following command and then close (in the case of /C) or return to the cmd prompt (in the case of /K)
startInfo.Arguments = $"/C WinAppDriver {port}";
https://ss64.com/nt/cmd.html

Related

How to run 2 files c# without opening window

I got 1 mapper (.exe) and 1 driver (.sys) and i want to do so that when i execute them i want to run mapper as admin with the driver and not create a window (keep it hidden). can anyone help. I have the following code down but nor does it run the mapper as admin with the spoof and it still also creates a window! help!
string map = "C:\\SCSpoofer\\mapper.exe";
Process myProcess = new Process();
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.CreateNoWindow = true;
myProcess.StartInfo.Verb = "runas";
myProcess = Process.Start(map, sys);
System.Threading.Thread.Sleep(150);
myProcess.Kill();```
// Create a Process to launch a command window (hidden) to create the item templates
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.WorkingDirectory = Project.ServicesFolder;
startInfo.Arguments = "/C " + CreateServices;
process.StartInfo = startInfo;
process.Start();
This is from my open source project DataTier.Net github.com/DataJuggler/DataTier.Net

Run taskmgr.exe without administrator rights?

I NEED to run Task Manager with the very specific code that I have, but it is appearing with an access denied error.
I have attempted to run in administrator mode before.
FileStream fs = new FileStream(System.IO.Path.Combine(Environment.SystemDirectory, "taskmgr.exe"), FileMode.Open, FileAccess.ReadWrite, FileShare.None);
The expected result I want is that Task Manager opens using the code above, without administrator rights! (Is there anyway around this?)
Use this:
using System.Diagnostics;
ProcessStartInfo startInfo = new ProcessStartInfo(); //a processstartinfo object
startInfo.CreateNoWindow = false; //just hides the window if set to true
startInfo.UseShellExecute = true; //use shell (current programs privillage)
startInfo.FileName = System.IO.Path.Combine(Environment.SystemDirectory, "taskmgr.exe"); //The file path and file name
startInfo.Arguments = ""; //Add your arguments here
Process.Start(startInfo);
Resources:
ProcessStartInfo - MSDN
This is a start process function I have
using System.Diagnostics;
private static void StartProcess(string exeName, string parameter)
{
using (Process process = new Process())
{
process.StartInfo.FileName = exeName;
process.StartInfo.Arguments = parameter;
process.EnableRaisingEvents = true;
process.Start();
}
}
Then call it like
StartProcess("exename.exe", fileParameter);
Process Class

C# Running command prompt as admin

I'm trying to run a few lines of command in an attempt to automate a work process. I've done some research online to get an idea of what I need to do. However the file remains unaffected. When I run the same command through admin command prompt, the command works.
Commands:
echo 127.0.0.1 webedit.egnyte.com >> "C:\Windows\System32\drivers\etc\hosts"
"%ProgramFiles(x86)%\Egnyte Connect\EgnyteDrive.exe" -command add -l Egnyte -d seagen -sso use-sso -t U -c connect_immediately
I've changed the manifest to "requireAdministrator" and "highestAvailable". However there still is no change to the file.
private void Button1_Click(object sender, EventArgs e)
{
string command1 = "echo 127.0.0.1 webedit.egnyte.com >> \"C:\\Windows\\System32\\drivers\\etc\\hosts\"";
Console.WriteLine("Starting Command 1: " + command1);
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = command1;
startInfo.UseShellExecute = true;
startInfo.Verb = "runas";
process.StartInfo = startInfo;
process.Start();
Console.WriteLine("Completed Command 1 ");
}
The file "host" should have an added line to it.

how to handle yes or no question while running a process

I am trying to execute a command by Process in c#, but problem is that that command ask a question (y/n) and process hang there. would you mind recommend me a solution?
public static OutputEventArgs execSync(string exe, string arguments)
{
OutputEventArgs oea = new OutputEventArgs();
try
{
using (Process myProcess = new Process())
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
startInfo.FileName = exe;
startInfo.Arguments = arguments;
myProcess.StartInfo = startInfo;
myProcess.Start();
oea.Data = myProcess.StandardOutput.ReadToEnd();
myProcess.WaitForExit();
oea.exitCode = myProcess.ExitCode;
}
}catch(Exception e)
{
oea.Data = e.Message;
oea.ExceptionHappened();
}
return oea;
}
my command output is something like below:
C:\Users\abc>pcli Label -prI:\PVCS\DEVELOPMENT\
-idabcd:abcpass -v'test3' -f -z '/Project1/APPLICATION/ajax_fetchGetCustNew.php' Unknown os = Windows
NT (unknown) Serena PVCS Version Manager (PCLI) v8.4.0.0 (Build 668)
for Windows NT/80x86 Copyright 1985-2010 Serena Software. All rights
reserved. Version "test3" is already defined in archive
"I:\PVCS\DEVELOPMENT\archives\Project1\Application\ajax_fetchGetCustNew.php-arc".
Overwrite? (y/n)
using(var myProcess = new Process()) {
...
myProcess.Start();
myProcess.StandardInput.WriteLine("y"); // Write 'y' to the processes' console input
...
}
Note: This approach is not very reusable.
Using a command line option like /no-confirm (as John Wu suggested in the questions comments) is preferable, if it exists.

How to run cmd command under administrator rights? [duplicate]

This question already has answers here:
How to start a Process as administrator mode in C# [duplicate]
(9 answers)
Closed 8 years ago.
How I can run below command under administrator approval in visual c#? Also I need to hide console windows while running console.
Thanks.
private void button5_Click(object sender, EventArgs e)
{
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C netsh wlan set hostednetwork mode=allow ssid=HotSpot key=12345678";
startInfo.Verb = "runas";
startInfo.UseShellExecute = true;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.StartInfo = startInfo;
process.Start();
Process wifiStart = new Process();
ProcessStartInfo wifiStartInfo = new ProcessStartInfo();
wifiStartInfo.FileName = "netsh.exe";
wifiStartInfo.Arguments = "/C wlan start hostednetwork";
wifiStartInfo.Verb = "runas";
wifiStartInfo.UseShellExecute = true;
wifiStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.StartInfo = wifiStartInfo;
wifiStart.Start();
}
Process.Start(new ProcessStartInfo {
FileName = "netsh",
Arguments = "wlan set hostednetwork mode=allow ssid=HotSpot key=12345678",
Verb = "runas",
UseShellExecute = true,
WindowStyle = ProcessWindowStyle.Hidden
});
This starts a process using the "runas" verb, which makes the shell try to execute it in elevated privileges mode. But we actually need the shell to be involved in this in the first place, hence the UseShellExecute = true value.
The last property tells the shell to hide the new process' window, but I'm not sure this will work for a console program.

Categories

Resources