Command Line Using c# Code - c#

I am starting a simple process using my c# code. I am able to open the cmd but not in the specified path and even the command is not executed, instead it shows The Handle is invalid error. It is done using simple winform application.
Below is my code:
var process = new System.Diagnostics.Process();
var startInfo = new System.Diagnostics.ProcessStartInfo
{
WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal,
WorkingDirectory = #"D:\Work\Application",
FileName = "cmd.exe",
RedirectStandardInput = true,
UseShellExecute = false
};
process.StartInfo = startInfo;
process.Start();
process.StandardInput.WriteLine("timeout /t 10");
It worked a day before but then it didnt work. I cant see what the problem is

All you have to do is:
string cmdText;
cmdText = "/C timeout /t 10";
System.Diagnostics.Process.Start("CMD.exe", cmdText);

Related

Open File from PowerShell via c#

In PowerShell this command: Invoke-Item D:\myFile.txt will open the file named myFile.
I'm tryind to run this PowerShell command From C#
I tried:
NuGet: System.Management.Automation
using (PowerShell PowerShellInstance = PowerShell.Create())
{
PowerShellInstance.AddScript("Invoke-Item D:\tx.txt");
PowerShellInstance.Invoke();
}
But this not opens the file
You can use this code:
Process process = new Process();
string scriptPath = $"YourPowershellCommand.ps1";
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = scriptPath,
RedirectStandardError = true;
RedirectStandardOutput = true;
CreateNoWindow = true;
UseShellExecute = false
};
process.StartInfo = startInfo;
process.Start();
var stderrx = process.StandardError.ReadToEnd();
var stdout = process.StandardOutput.ReadToEnd();
process.WaitForExit();
Just save your powershell command in a ps1 file and give the path as a parameter. The code is not tested but it should work. Also C# process is in System.Diagnostics namespace.

How to run a command tool exe in C# web application?

I am using graphicmagic exe to execute a command using command prompt. I have added the graphicmagic exe in my application root folder. I want to execute this exe and pass the arguments through c#. How to do this? I have tried the below code:
Method: 1
Process proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = AppDomain.CurrentDomain.BaseDirectory + "\\gm1.3.5\\gm",
Arguments = "convert -define dpx:colorspace=rgb -define tiff:bits-per-sample=16 'D:\\Image\\FFv1\\dpx1\\1.dpx' 'D:\\Image\\FFv1\\tiff1\\1.tiff'",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
}
Method: 2
Process proc = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = #"D:\Executable\Projects\MMF\gm1.3.5\gm";
startInfo.Arguments = "convert -define dpx:colorspace=rgb -define tiff:bits-per-sample=16 \"D:\\Image\\FFv1\\dpx1\\1.dpx\" \"D:\\Image\\FFv1\\tiff1\\1.tiff\"";
proc.StartInfo = startInfo;
proc.Start();
But both of its not working. Please suggest a way to execute exe file and pass commands.
It should be the privilege issue.
Try to adjust the identity in your app pool to LocalSystem.
This one is working fine:
using System.Diagnostics;
Process p = new Process();
p.StartInfo.FileName = #"D:\Executable\Projects\MMF\gm1.3.5\gm.exe";
p.StartInfo.Arguments = "convert -define dpx:colorspace=rgb -define tiff:bits-per-sample=16 \"C:\\Documents and Settings\\software\\Desktop\\DPX_Test\\3.dpx\" \"C:\\TEMP_21May2015103216\\3.tiff\"";
p.Start();
p.WaitForExit();

getting a blank cmd window by running command line from C#

I am trying to open command line in the c folder, from C#.
the expectation is to see this in the command line window:
C:>
but instead i am getting a blank cmd window.
this is the code:
var startInfo = new System.Diagnostics.ProcessStartInfo
{
WorkingDirectory = #"c:\",
WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal,
FileName = "cmd.exe",
RedirectStandardInput = true,
UseShellExecute = false
};
Console.ReadKey();
WaitForExit is what you are looking for.
EDIT:
var startInfo = new System.Diagnostics.ProcessStartInfo
{
WorkingDirectory = #"c:\",
WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal,
FileName = "cmd.exe",
RedirectStandardInput = true,
UseShellExecute = false
};
Process p = new Process();
p.StartInfo = startInfo;
p.Start();
p.WaitForExit();
This will halt the execution of all statements after WaitForExit. The time you close the command window, the statements following WaitForExit will be executed.
i think you're looking for this
first we'll create a process for CMD.exe and then passes "/K cd \". "/K" will "CMD.exe" to receive parameter and stay open, while "cd \" will take us to "C:/" which is your requirement
System.Diagnostics.Process.Start("CMD.exe", "/K \"cd /\"");
Console.ReadKey();
I believe if you use the /K Argument when executing the command, you should have a command prompt running at C:\
ProcessStartInfo startinfo = new ProcessStartInfo();
startinfo.FileName = "cmd.exe";
startinfo.WorkingDirectory = #"C:\";
startinfo.Arguments = "/K";
startinfo.UseShellExecute = false;
Process.Start(startinfo);
Or
Process command = new Process();
command.StartInfo.UseShellExecute = false;
command.StartInfo.WorkingDirectory = #"C:\";
command.StartInfo.Arguments = "/K";
command.StartInfo.FileName = "cmd.exe";
command.Start();
The /K argument executes the cmd.exe command and keeps the window open :)

how to run a python code using cmd.exe from a C# application?

I have an application where i am trying to run a python from a c# application. i have tried creating the python runtime environment and run the code, but as my python code is importing some modules from another python file it throws an exception (import exception). i have tried the following code:
var ipy = Python.CreateRuntime();
dynamic test = ipy.UseFile(#"file path");
test.Simple();
Console.Read();
I hvae another idea of running it through cmd prompt, but i don't know how do it. i want open to cmd.exe and execute the python file and i want it such that the user enters the filename in c# aplication and on clicking the run button the code is executed through cmd.exe and the output is again shown in c# application.
Any other suggestions are also welcome.
That would do the job: the following example runs cmd which runs TCL script (that wat I have installed on my computer) you only need to replace the command to run Python and add your script file.
Pay attention to the " & exit" comming after your script file name - this makes the cmd exit after your script exits.
string fileName = "C:\\Tcl\\example\\hello.tcl";
Process p = new Process();
p.StartInfo = new ProcessStartInfo("cmd", "/K tclsh " + fileName + " & exit")
{
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
};
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
Console.WriteLine(output);
Console.ReadLine();
[Update]
After Python installation and testing, that would be the code to run python script with cmd:
string fileName = #"C:\Python27\example\hello_world.py";
Process p = new Process();
p.StartInfo = new ProcessStartInfo("cmd", "/K " + fileName + " & exit")
{
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
};
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
Console.WriteLine(output);
Console.ReadLine();
Also you can do the same without the CMD process:
string fileName = #"C:\Python27\example\hello_world.py";
Process p = new Process();
p.StartInfo = new ProcessStartInfo(#"C:\Python27\python.exe", fileName )
{
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
};
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
Console.WriteLine(output);
Console.ReadLine();
I cannot test it personally at the moment, but I found some people using Python.CreateEngine() in their code, example:
Microsoft.Scripting.Hosting.ScriptEngine engine =
IronPython.Hosting.Python.CreateEngine();
This line was taken from this SO question.
You can also check this article with example class using python code. It also uses Python.CreateEngine().
i have tried the following code and it seems to solve my problem:
Process p = new Process();
string cmd = #"python filepath & exit";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.RedirectStandardInput = true;
p.Start();
StreamWriter myStreamWriter = p.StandardInput;
myStreamWriter.WriteLine(cmd.ToString());
myStreamWriter.Close();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
Console.ReadLine();

redirect any results in command prompt to richtext box

I need to redirect any results in command prompt to richtext box. Can any one provide me the necessary steps. This is how i start my command prompt.
ProcessStartInfo psi = new ProcessStartInfo
{
FileName = "cmd",
Arguments = #"/k ""C:\Program Files\Microsoft Visual Studio 9.0\VC\bin\vcvars32.bat""",
};
Process.Start(psi);
var process = new Process
{
StartInfo = new ProcessStartInfo("cmd.exe", "/C dir c:")
{
RedirectStandardOutput = true,
UseShellExecute = false,
}
};
process.Start();
string output = process.StandardOutput.ReadToEnd();
process.WaitForExit();
Console.WriteLine(output);
EDIT: Then you can try this
var process = new Process
{
StartInfo = new ProcessStartInfo("cmd.exe")
{
RedirectStandardOutput = true,
RedirectStandardInput = true,
UseShellExecute = false,
}
};
process.Start();
// Discard "Microsoft windows all rights reserved etc.
while (process.StandardOutput.ReadLine() != "") ;
// Run command
process.StandardInput.WriteLine("dir c:");
// Skip command entered
process.StandardOutput.ReadLine();
// And exit
process.StandardInput.WriteLine("exit");
process.WaitForExit();
string output = process.StandardOutput.ReadToEnd();
Console.WriteLine(output);
You're going to want to redirect the stdout stream from the Process. I don't remember the exact properties and methods involved, but take a look through the MSDN documentation on Process.

Categories