When I am running relog command from command prompt, I am getting the output without any issue.
Now when I am trying to run command using C# code, I am not getting any output. What could be the issue?
public static string Bash()
{
var process = new Process()
{
StartInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
WorkingDirectory = #"C:\blgs",
Arguments = "relog.exe sample.blg",
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true,
}
};
process.Start();
string result = process.StandardOutput.ReadToEnd();
process.WaitForExit();
return result;
}
Related
I have written a console application to call the AZcopy tool programmatically, but I was not able to call azcopy tool. Can you please redirect me the workable source ? I have downloaded azcopy_windows_amd64_10.12.1 and put azcopy.exe into C:\Windows\System32\azcopy.exe location
static void Main(string[] args)
{
string strCmdText = #"AzCopy.exe sync ""D:\temp"" ""https://myhubforazcopy.blob.core.windows.net/myhubforazcopy1?sp=racwdl&st=2021-09-05T17:19:11Z&se=2021-09-06T01:19:11Z&spr=https&sv=2020-08-04&sr=c&sig=MAraJ0PxqJMDdYuWzrOUEwYda%2BkXEukP%2Fs%3D"" --destination-delete=true";
CallProcess(strCmdText);
}
public static void CallProcess(string strCmdText)
{
//C:\Windows\System32\azcopy.exe
var process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = #"C:\Windows\System32\cmd.exe",
RedirectStandardInput = true,
RedirectStandardOutput = true,
Arguments = strCmdText,
UseShellExecute = false,
CreateNoWindow = false,
WorkingDirectory = #"D:\AzCopy\bin\Debug\"
};
process.StartInfo = startInfo;
process.Start();
process.StandardInput.Flush();
process.StandardInput.Close();
process.WaitForExit();
Console.WriteLine(process.StandardOutput.ReadToEnd());
}
Starting cmd with the program you want to execute as the argument doesn't actually runt that program, you'll need to pass the entire argument (your program) after the /c flag, like so:
string strCmdText = #"/c AzCopy.exe sync ""D:\temp"" ...
Make sure that AzCopy is in your PATH or specify the precise path to it
But it would be better if you just directly run AzCopy instead of starting a command prompt which then starts AzCopy, this would be done like so:
// Notice how the 'azcopy' at the start is gone
var arguments = #"""D:\temp"" ""https://myhubforazcopy.blob ..."
var process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = #"C:\Windows\System32\azcopy.exe",
RedirectStandardInput = true,
RedirectStandardOutput = true,
Arguments = arguments,
UseShellExecute = false,
CreateNoWindow = false,
WorkingDirectory = #"D:\AzCopy\bin\Debug\"
};
Solution :
static void Main(string[] args)
{
string strCmdText = #"/c azcopy.exe sync ""D:\temp"" ""https://myhubforazcopy.blob.core.windows.net/myhubforazcopy1?sp=racwdl&st=2021-09-05T17:19:11Z&se=2021-09-06T01:19:11Z&spr=https&sv=2020-08-04&sr=c&sig=MAraJ0PxqJMDdYuWzrOUEwYda%2BkXEukP%2Fs%3D"" --delete-destination=true";
CallProcess(strCmdText);
}
public static void CallProcess(string strCmdText)
{
var process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = #"cmd.exe",
RedirectStandardInput = true,
RedirectStandardOutput = true,
Arguments = strCmdText,
UseShellExecute = false,
CreateNoWindow = false,
WorkingDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
};
process.StartInfo = startInfo;
process.Start();
process.StandardInput.Flush();
process.StandardInput.Close();
process.WaitForExit();
Console.WriteLine(process.StandardOutput.ReadToEnd());
}
Hi have a service which needs to execute it's updated shell script independent from the .Core process. I have tried /bin/bash, /bin/nohup and /bin/setsid. But every time the script stops the systemd service the script seems also to be stopped. How do I get this independent?
private static Process StartLinuxUpdateScript(string pathToUpdateScript, string pathToUpdateZip)
{
var process = new Process()
{
StartInfo = new ProcessStartInfo
{
FileName = "/bin/setsid",
UseShellExecute = true,
Arguments = $" bash {pathToUpdateScript} {pathToUpdateZip}"
}
};
return process;
}
maybe it helpfull:
public void RunProcess(string fileName, string arg)
{
var process = new Process
{
StartInfo =
{
RedirectStandardOutput = true,
RedirectStandardError = true,
FileName = fileName,
Arguments = arg ,
UseShellExecute = false,
CreateNoWindow = true
},
EnableRaisingEvents = true
};
process.Start();
string processOutput;
while ((processOutput = process.StandardError.ReadLine()) != null)
{
Console.WriteLine(processOutput);
}
process.Dispose();
}
I wrote an app in C# (WPF) which takes remote hosts data (using Psexec).
The app requires you to be with high privileges (Administrator).
I have this kind of code in my app:
var proc = new Process {
StartInfo = new ProcessStartInfo {
FileName = "psexec.exe",
Arguments = "\\\\" + ip + " ipconfig",
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true
}
};
proc.Start();
if (!proc.WaitForExit(60000))
proc.Kill();
output_error = proc.StandardError.ReadToEnd();
output_stan = proc.StandardOutput.ReadToEnd();
If i'm running the app from Visual Studio (In debug mode), i get an output, but when i'm running the app from the exe file the standard redirected output which is just empty.
Does anyone has a possible solution for this?
*The output which is redirected as an error is a standrad psexec output which says basiclly that the command worked just find (error 0).
Thx.
From MSDN:
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "Write500Lines.exe";
p.Start();
// To avoid deadlocks, always read the output stream first and then wait.
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
In particular note how you shouldn't wait before reading to the end of the stream otherwise you might be getting deadlocks.
I have modified your code to do it this way, and the following works fine for me:
static void Main(string[] args)
{
var proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "ping.exe",
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true
}
};
proc.Start();
string output_error = proc.StandardError.ReadToEnd();
string output_stan = proc.StandardOutput.ReadToEnd();
proc.WaitForExit();
Trace.TraceInformation(output_stan);
}
I'm trying to run the following code in my C# WPF application. Whenever I use something like dir (I should probably mention the output is the directory of my working folder in Visual Studio, not System32), it allows it. However, if I use systeminfo or set the working directory to C:\Windows\System32, it hangs...
MessageBox.Show("STARTED");
var processInfo = new ProcessStartInfo("cmd.exe", "/c systeminfo") {
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardError = true,
RedirectStandardOutput = true,
//WorkingDirectory = #"C:\Windows\System32\"
};
// *** Redirect the output ***
Process process = Process.Start(processInfo);
if (process == null) return false;
process.WaitForExit();
MessageBox.Show("Done");
string output = process.StandardOutput.ReadToEnd().ToLower();
string error = process.StandardError.ReadToEnd();
int exitCode = process.ExitCode;
MessageBox.Show(output);
MessageBox.Show(error);
MessageBox.Show(exitCode.ToString(CultureInfo.InvariantCulture));
Any ideas?
Just tried this and works as expected.
Of course you need to read the redirected output before the process closes.
var processInfo = new ProcessStartInfo("cmd.exe", "/c systeminfo")
{
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardError = true,
RedirectStandardOutput = true,
WorkingDirectory = #"C:\Windows\System32\"
};
StringBuilder sb = new StringBuilder();
Process p = Process.Start(processInfo);
p.OutputDataReceived += (sender, args) => sb.AppendLine(args.Data);
p.BeginOutputReadLine();
p.WaitForExit();
Console.WriteLine(sb.ToString());
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.