c# start console application in win forms - c#

I'm trying to make launcher for my games and I like to add music player in background, but if I start the process it instantly fail's.
Code
private void btnStartMusic_Click(object sender, EventArgs e)
{
ProcessStartInfo proc = new
ProcessStartInfo("MusicPlayer\\MemequickieMusicPlayer.exe");
proc.CreateNoWindow = true;
proc.WindowStyle = ProcessWindowStyle.Hidden;
proc.RedirectStandardError = false;
proc.RedirectStandardInput = false;
proc.RedirectStandardOutput = false;
proc.UseShellExecute = false;
Process.Start(proc);
}
Any Help is appreciated.

Try using the full path to the exe and setting the working directory; assuming the exe is in your executable folder:
string path = Path.Combine(Application.StartupPath, "MusicPlayer");
ProcessStartInfo proc = new
ProcessStartInfo(Path.Combine(path, "MemequickieMusicPlayer.exe"));
proc.WorkingDirectory = path;
If the error persists and you want to debug the output, change:
proc.RedirectStandardOutput = true;
Create the process like this:
Process process = new Process(proc);
process.Start();
while (!process.StandardOutput.EndOfStream) {
System.Diagnostics.Debug.Write(process.StandardOutput.ReadLine());
}
You should now see the output in your output window.

Related

Run a C# exe with parameters that is to start another application and get output from console

I have an exe which has some parameters- path of another application and some files to be opened from that application. There would be an output as part of that application which would be displayed in the console of my exe.
But i am unable to get the output from the console.
I have the code:
ProcessStartInfo psi = new ProcessStartInfo("\"" + dllpath + "\\newapplication.exe" + "\"");
Process p = new Process();
psi.UseShellExecute = false;
psi.RedirectStandardInput = true;
psi.RedirectStandardOutput = true;
p.Start();
The process starts successfully, and then i have to open a file in the process which happens through another class. So after the file opened, some extraction happens and the result is displayed on the console.
When i give p.WaitForExit(); nothing happens other than starting the application! How do i acheive to retreive the output on StandardOutput as per my code? Need Help!
This is the correct way to do it:
string outputProcess = "";
string errorProcess = "";
using (Process process = new Process())
{
process.StartInfo.FileName = path;
process.StartInfo.Arguments = arguments;
process.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.Start();
outputProcess = process.StandardOutput.ReadToEnd();
errorProcess = process.StandardError.ReadToEnd();
process.WaitForExit();
}
Remember to use the using statement when you have an IDisposable object

Windows Form app in Adminstrator mode

I have windows form application which needs to be run in administrator mode with out editing the app.manifest file. Got the below codebut don't know where to place the code in windows form application.
ProcessStartInfo proc = new ProcessStartInfo();
proc.UseShellExecute = true;
proc.WorkingDirectory = Environment.CurrentDirectory;
proc.FileName = Application.ExecutablePath;
proc.Verb = "runas";
Process.Start(proc);
Application.Exit(); // Quit itself
Put this code inside you main form loading event
private void Form1_Load(object sender, EventArgs e)
{
ProcessStartInfo proc = new ProcessStartInfo();
proc.UseShellExecute = true;
proc.WorkingDirectory = Environment.CurrentDirectory;
proc.FileName = Application.ExecutablePath;
proc.Verb = "runas";
Process.Start(proc);
Application.Exit(); // Quit itself
}

How do i start a process in c# calling net.exe without having a window pop open?

I am starting a process to disconnect a network drive, but it always pops up a command prompt window. It just flashes by quickly and disappears, but i would like to configure it so the window doesn't open at all. Any ideas?
Here is the c# code I'm currently using:
private void btnDisconnectNetwork_Click(object sender, EventArgs e)
{
Process DisconnectDrive = new Process();
DisconnectDrive.StartInfo.FileName = "Net.exe";
DisconnectDrive.StartInfo.Arguments = #" Use /d Q:";
DisconnectDrive.StartInfo.CreateNoWindow = true;
DisconnectDrive.Start();
}
I believe the following will also work:
using System.Diagnostics;
namespace processexample {
class Program {
static void Main(string[] args) {
ProcessStartInfo si = new ProcessStartInfo();
si.CreateNoWindow = true;
si.UseShellExecute = false;
si.FileName = #"C:\Windows\System32\net.exe";
si.Arguments = #"/help";
Process p = new Process();
p.StartInfo = si;
p.Start();
}
}
}
You have to set the CreateNoWindow and UseShellExecute in StartInfo.

How can I run a batch file from my Visual C# .NET code and retrieve the results?

I have created a getSvnUrl.bat
containing the following code
#ECHO OFF
for /f "tokens=1-2" %%i in ('"%VISUALSVN_SERVER%\bin\svn.exe" info C:\\Code\\ServiceRouter153\\SCM\\Configs\\ServiceRouter_MIGRATION.xml') do (
if "%%i"=="URL:" (
set URL=%%j
)
)
echo The result is.. %URL%
I would like to run this .bat from C# .NET code and be able to collect the SVN url contained the URL variable in a String variable in C#.
I tried this C# code from an 11 year old MSN blog http://blogs.msdn.com/b/csharpfaq/archive/2004/06/01/146375.aspx
It is not working. Is there a new way to do this?
private void runSyncAndGetResults_Click(object sender, System.EventArgs e)
{
System.Diagnostics.ProcessStartInfo psi =
new System.Diagnostics.ProcessStartInfo(#"C:\getSvnUrl.bat");
psi.RedirectStandardOutput = true;
psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
psi.UseShellExecute = false;
System.Diagnostics.Process listFiles;
listFiles = System.Diagnostics.Process.Start(psi);
System.IO.StreamReader myOutput = listFiles.StandardOutput;
listFiles.WaitForExit(2000);
if (listFiles.HasExited)
{
string output = myOutput.ReadToEnd();
this.processResults.Text = output;
}
}
Here is the method I ended up using in case it ever helps someone else.
private string runBatchGetSvnUrl()
{
System.Diagnostics.ProcessStartInfo psi =
new System.Diagnostics.ProcessStartInfo(#"C:\getSvnUrl.bat");
psi.RedirectStandardOutput = true;
psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
psi.UseShellExecute = false;
System.Diagnostics.Process listFiles;
listFiles = System.Diagnostics.Process.Start(psi);
System.IO.StreamReader myOutput = listFiles.StandardOutput;
listFiles.WaitForExit(2000);
if (listFiles.HasExited)
{
string output = myOutput.ReadToEnd();
return output;
}
else
{
return "It didn't work.";
//change this to throw an excetion later
}
}

How to run a batch file within a C# GUI form

How would you execute a batch script within a GUI form in C#
Could anyone provide a sample please?
System.Diagnotics.Process.Start("yourbatch.bat"); ought to do it.
Another thread covering the same issue.
This example assumes a Windows Forms application with two text boxes (RunResults and Errors).
// Remember to also add a using System.Diagnostics at the top of the class
private void RunIt_Click(object sender, EventArgs e)
{
using (Process p = new Process())
{
p.StartInfo.WorkingDirectory = "<path to batch file folder>";
p.StartInfo.FileName = "<path to batch file itself>";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.Start();
p.WaitForExit();
// Capture output from batch file written to stdout and put in the
// RunResults textbox
string output = p.StandardOutput.ReadToEnd();
if (!String.IsNullOrEmpty(output) && output.Trim() != "")
{
this.RunResults.Text = output;
}
// Capture any errors written to stderr and put in the errors textbox.
string errors = p.StandardError.ReadToEnd();
if (!String.IsNullOrEmpty(errors) & errors.Trim() != ""))
{
this.Errors.Text = errors;
}
}
}
Updated:
The sample above is a button click event for a button called RunIt. There's a couple of text boxes on the form, RunResults and Errors where we write the results of stdout and stderr to.
I deduce that by executing within a GUI form you mean showing execution results within some UI-Control.
Maybe something like this:
private void runSyncAndGetResults_Click(object sender, System.EventArgs e)
{
System.Diagnostics.ProcessStartInfo psi =
new System.Diagnostics.ProcessStartInfo(#"C:\batch.bat");
psi.RedirectStandardOutput = true;
psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
psi.UseShellExecute = false;
System.Diagnostics.Process batchProcess;
batchProcess = System.Diagnostics.Process.Start(psi);
System.IO.StreamReader myOutput = batchProcess.StandardOutput;
batchProcess.WaitForExit(2000);
if (batchProcess.HasExited)
{
string output = myOutput.ReadToEnd();
// Print 'output' string to UI-control
}
}
Example taken from here.

Categories

Resources