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();
Related
I want to run .exe file from another app, which is console app in .NET Core. When the app is open I want to write input to console from my code. Something like this:
var cmd = System.Diagnostics.Process.Start("myApp.exe");
cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.RedirectStandardOutput = true;
cmd.StartInfo.UseShellExecute = false;
cmd.Start();
cmd.StandardInput.WriteLine("MyName"); // this should be entered in console as username
So I specify username from my code, instead of writing it to console manually. Code above is not working for me. Is there a way to do this?
enter username
You're first starting the Process and then you're manipulating the startup arguments. You need to start the process afterwards.
use something like this instead:
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.UseShellExecute = false; //required to redirect standart input/output
// redirects on your choice
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.FileName = ...app path to execute...;
startInfo.Arguments = ...argumetns if required...;
Process process = new Process();
process.StartInfo = startInfo;
process.Start();
process.StandardInput.WriteLine(...write whatever you want...);
source
You need redirect standard input. Create ProcessStartInfo structure, then start a process and after process started write to the process standart input
ProcessStartInfo processStartInfo = new ProcessStartInfo
{
FileName = pathToApplication,
RedirectStandardInput = true,
UseShellExecute = false
};
Process process = Process.Start(processStartInfo);
var writer = process.StandardInput;
writer.WriteLine("MyName");
Console.ReadLine();
This is my code:
string damnfile = System.IO.Path.GetDirectoryName(choofdlog.FileName);
ProcessStartInfo startInfo = new ProcessStartInfo("% ProgramFiles %\\Windows Defender\\MpCmdRun.exe");
startInfo.WindowStyle = ProcessWindowStyle.Maximized;
startInfo.Arguments = "-Scan -ScanType 3 -File" + damnfile;
Process p = new Process();
p.StartInfo = startInfo;
p.Start();
As you can see, I want to run WinDefender scan for a specific file, but it throws error that the process is not found, but this code works:
ProcessStartInfo startInfo = new ProcessStartInfo("netstat");
startInfo.WindowStyle = ProcessWindowStyle.Maximized;
startInfo.Arguments = "-a";
Process p = new Process();
p.StartInfo = startInfo;
p.Start();
Also I want in the second code to redirect the cmd output to listBox, but thats the second problem, first help me with the defender scan please.
Solveded like now its opening, because I replaced
ProcessStartInfo startInfo = new ProcessStartInfo("% ProgramFiles %\\Windows Defender\\MpCmdRun.exe");
with
ProcessStartInfo startInfo = new ProcessStartInfo(#"C:\Program Files\\Windows Defender\\MpCmdRun.exe");
Now appeared another problem: even I passed some arguments, the windows immediately close.
I've tried the snippet of code:
Process p = new Process();
ProcessStartInfo info = new ProcessStartInfo("cmd.exe");
info.UseShellExecute = true;
info.Verb = "runas";
p.StartInfo = info;
p.Start();
using (StreamWriter sw = p.StandardInput)
{
if (sw.BaseStream.CanWrite)
{
sw.WriteLine("DISM.exe /Online /Add-Capability /CapabilityName:Microsoft.WebDriver~~~~0.0.1.0");
}
}
It successfully opens up a command prompt with administrative privileges but I can't seem to write into it use streamwriter. I know that this is because I don't have RedirectStandardInput set to true. If I do set this to true, I am able to write but my console loses its elevated privileges. My question is how can I write a command into the console when UseShellExecute is true?
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);
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 :)