I have a console application that asks for a SourcePath when started.. when I enter the Source Path, It asks for DestinationPath... when i enter DestinationPath it starts some execution
My Problem is to Supply these path via a windows application, means i need to create a window form application that will supply these parameters to the console application automatiocally after certain time interval
can it be achieved or not... if yes, plese help... its very Urgent...
ohh..
I have tried a lot of code that i can not paste hear all but some that i use to start the application are...
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = #"C:\Program Files\Wondershare\PPT2Flash SDK\ppt2flash.exe";
psi.UseShellExecute = false;
psi.RedirectStandardError = true;
psi.RedirectStandardInput = true;
psi.CreateNoWindow = false;
psi.Arguments = input + ";" + output;
Process p = Process.Start(psi);
and
Process process = new Process
{
StartInfo = new ProcessStartInfo
{
CreateNoWindow = true,
FileName = #"C:\Program Files\Wondershare\PPT2Flash SDK\ppt2flash.exe",
RedirectStandardError = true,
RedirectStandardOutput = true,
UseShellExecute = false,
}
};
if (process.Start())
{
Redirect(process.StandardError, text);
Redirect(process.StandardOutput, text);
MessageBox.Show(text);
}
private void Redirect(StreamReader input, string output)
{
new Thread(a =>{var buffer = new char[1];
while (input.Read(buffer, 0, 1) > 0)
{
output += new string(buffer);
};
}).Start();
}
but nothing seems to be working
You can add parameters to your ProcessStartInfo like this:
ProcessStartInfo psi = new ProcessStartInfo(#"C:\MyConsoleApp.exe",
#"C:\MyLocationAsFirstParamter C:\MyOtherLocationAsSecondParameter");
Process p = Process.Start(psi);
this will startup the console app with 2 parameters.
Now in your console app you have the
static void Main(string[] args)
the string array args is what contains the parameters, now all you have to do is get them when your app starts.
if (args == null || args.Length < 2)
{
//the arguments are not passed correctly, or not at all
}
else
{
try
{
yourFirstVariable = args[0];
yourSecondVariable = args[1];
}
catch(Exception e)
{
Console.WriteLine("Something went wrong with setting the variables.")
Console.WriteLine(e.Message);
}
}
This may or may not be the exact code that you need, but at least will give you an insight in how to accomplish what you want.
Related
I am attempting to run a windows command (e.g. whoami) without calling cmd.exe (or powershell) directly using C#.
Within VB this is possible using CreateObject(WScript.Shell) it obviously does not have to be the same method as within the VB, although that would be nice, but I just do not want to call cmd.exe directly.
How would I be able to achieve this?
This runs a console program, waits for exit and reads the output. I changed the cmd to ping since that takes longer and I can verify no console window opens.
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "ping.exe";
startInfo.Arguments = "google.com";
startInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
// This wasn't needed
//startInfo.WindowStyle = ProcessWindowStyle.Hidden;
Process processTemp = new Process();
processTemp.StartInfo = startInfo;
processTemp.EnableRaisingEvents = true;
try
{
processTemp.Start();
textBox1.Text = processTemp.StandardOutput.ReadToEnd();
processTemp.WaitForExit();
}
catch (Exception ex)
{
textBox1.Text = ex.Message;
}
You could call whoami.exe and capture the output directly.
The key is UseShellExecute = false to run the executable directly.
var proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = #$"{Environment.ExpandEnvironmentVariables("%systemroot%")}\system32\whoami.exe",
Arguments = // Put any command line arguments here
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};
proc.Start();
string line = proc.StandardOutput.ReadToEnd();
I am trying to create a child process from a c# console app that shows the console. I tried the following but no window appeared.
ProcessStartInfo = new ProcessStartInfo(name)
{
UseShellExecute = false,
RedirectStandardError = true,
RedirectStandardOutput = true,
WindowStyle = ProcessWindowStyle.Maximized,
CreateNoWindow = false,
ErrorDialog = false
};
if (args != null)
{
ProcessStartInfo.Arguments = args;
}
if (workingDirectory != null)
{
ProcessStartInfo.WorkingDirectory = workingDirectory;
}
Process = new Process {EnableRaisingEvents = true, StartInfo = ProcessStartInfo};
Process.Start();
The right way to run child process in the parent’s console is to setup UseShellExecute property of ProcessStartInfo class. Let’s consider an example that executes time command. Why time? Because it reads from standard input. This way you will know which console it uses.
public class Program
{
public static void Main(string[] args)
{
var processInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
Arguments = "/c time"
};
Console.WriteLine("Starting child process...");
using (var process = Process.Start(processInfo))
{
process.WaitForExit();
}
}
}
We left the default value of UseShellExecute, which is true. It means that a shell will be used for the child process. Using shell means that a new console will be created.
Let’s flip the value of UseShellExecute to false.
public class Program
{
public static void Main(string[] args)
{
var processInfo = new ProcessStartInfo
{
UseShellExecute = false, // change value to false
FileName = "cmd.exe",
Arguments = "/c time"
};
Console.WriteLine("Starting child process...");
using (var process = Process.Start(processInfo))
{
process.WaitForExit();
}
}
}
I am trying to implement piping behavior in C#.NET using Process and ProcessStartInfo classes. My intention is to feed the output of previous command to next command so that that command will process the input and return result. have a look into following code -
private static string Out(string cmd, string args, string inputFromPrevious)
{
string RetVal = string.Empty;
try
{
ProcessStartInfo psi = String.IsNullOrEmpty(args) ? new ProcessStartInfo(cmd) : new ProcessStartInfo(cmd, args);
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.UseShellExecute = false;
psi.RedirectStandardInput = string.IsNullOrEmpty(inputFromPrevious) ? false : true;
Process proc = new System.Diagnostics.Process();
proc.StartInfo = psi;
if (proc.Start())
{
if (!string.IsNullOrEmpty(inputFromPrevious))
{
proc.StandardInput.AutoFlush = true;
proc.StandardInput.WriteLine(inputFromPrevious); proc.StandardInput.WriteLine();
proc.StandardInput.Close();
}
using (var output = proc.StandardOutput)
{
Console.WriteLine("3. WAITING WAITING....."); < ---Code halts here.
RetVal = output.ReadToEnd();
}
}
proc.WaitForExit();
proc.Close();
}
catch (Exception Ex)
{
}
return RetVal; < ---This is "inputFromPrevious" to next call to same function
}
I am calling above code in loop for different commands and arguments. Is there any mistake in my code? I had looked similar as shown below but nothing worked for me as of now. Any help will be appreciated.
Piping in a file on the command-line using System.Diagnostics.Process
Why is Process.StandardInput.WriteLine not supplying input to my process?
Repeatably Feeding Input to a Process' Standard Input
Can I start a process (using C# Process.Start()) in the same console as the calling program? This way no new window will be created and standard input/output/error will be the same as the calling console application. I tried setting process.StartInfo.CreateNoWindow = true; but the process still starts in a new window (and immediately closes after it finishes).
You shouldn't need to do anything other than set UseShellExecute = false, as the default behaviour for the Win32 CreateProcess function is for a console application to inherit its parent's console, unless you specify the CREATE_NEW_CONSOLE flag.
I tried the following program:
private static void Main()
{
Console.WriteLine( "Hello" );
var p = new Process();
p.StartInfo = new ProcessStartInfo( #"c:\windows\system32\netstat.exe", "-n" )
{
UseShellExecute = false
};
p.Start();
p.WaitForExit();
Console.WriteLine( "World" );
Console.ReadLine();
}
and it gave me this output:
You could try redirecting the output of this process and then printing it on the calling process console:
public class Program
{
static void Main()
{
var psi = new ProcessStartInfo
{
FileName = #"c:\windows\system32\netstat.exe",
Arguments = "-n",
RedirectStandardOutput = true,
UseShellExecute = false
};
var process = Process.Start(psi);
while (!process.HasExited)
{
Thread.Sleep(100);
}
Console.WriteLine(process.StandardOutput.ReadToEnd());
}
}
Alternative approach using the Exited event and a wait handle:
static void Main()
{
using (Process p = new Process())
{
p.StartInfo = new ProcessStartInfo
{
FileName = #"netstat.exe",
Arguments = "-n",
RedirectStandardOutput = true,
UseShellExecute = false
};
p.EnableRaisingEvents = true;
using (ManualResetEvent mre = new ManualResetEvent(false))
{
p.Exited += (s, e) => mre.Set();
p.Start();
mre.WaitOne();
}
Console.WriteLine(p.StandardOutput.ReadToEnd());
}
}
I have a app that is doing some video processing.
I need to analyze the media before processing it.
The ffmpeg utility ffprobe.exe provides all the information I need.
However the code I am using will not return the text that appears when the command is ran in a cmd window :
public static string RunConsoleCommand(string command, string args)
{
var consoleOut = "";
using (var process = new Process())
{
process.StartInfo = new ProcessStartInfo
{
FileName = command,
Arguments = args,
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardOutput = true
};
process.Start();
consoleOut = process.StandardOutput.ReadToEnd();
process.WaitForExit();
return consoleOut;
}
}
Any Ideas?
The Process class has some events to handle that :
public static string RunConsoleCommand(string command, string args)
{
var consoleOut = "";
using (var process = new Process())
{
process.StartInfo = new ProcessStartInfo
{
FileName = command,
Arguments = args,
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardOutput = true
};
// Register for event and do whatever
process.OutputDataReceived += new DataReceivedEventHandler((snd, e) => { consoleOut += e.Data; });
process.Start();
process.WaitForExit();
return consoleOut;
}
}
You also have ErrorDataReceived, which works the same way.
I'm using these events in some projects, and it works like a charm. Hope that helps.
EDIT : Fixed the code, you need to attach the handlers before you start the process.