Antoine Pelletier
Antoine Pelletier

Reputation: 3326

Cmd with c# console app, does these commands work?

Well... everything is in the title, this is a C# console app and i'm trying to execute these commands... it dosen't seems to work but why ?

It just open a cmd, i don't see anything like a command

static void Main(string[] args)
    {
        string strCmdText = "mysqlcheck -r JAMFSOFTWARE -u root -p password";
        System.Diagnostics.Process.Start("CMD.exe", strCmdText);

        string strCmdText2 = "mysqlcheck -o JAMFSOFTWARE -u root -p password";
        System.Diagnostics.Process.Start("CMD.exe", strCmdText);

        string strCmdText3 = "mysqlcheck -c JAMFSOFTWARE -u root -p password";
        System.Diagnostics.Process.Start("CMD.exe", strCmdText);

    }

Edit : The thing is i don't want to put that password in a batch file where it would be easely accessible, so basically any solution would do as long as no one can see what's in the commande file

GOT CLOSER TO A SOLUTION :

    static void Main(string[] args)
    {

        ProcessStartInfo startInfo = new ProcessStartInfo("mysqlcheck");
        startInfo.Arguments = "-c JAMFSOFTWARE -u root -p !!fakepassword!!";


        Process process = new Process();
        process.StartInfo = startInfo;

        process.OutputDataReceived += new DataReceivedEventHandler(process_OutputDataReceived);

        try
        {
            process.Start();

        }
        catch
        {
        }

    }
    static void process_OutputDataReceived(object sender, DataReceivedEventArgs e)
    {
        StreamWriter sw = new StreamWriter("C:\\JAMFCHECK.txt");
        sw.WriteLine( e.Data);
    }

now i don't know why but it ask for a password...

Upvotes: 0

Views: 128

Answers (2)

Robin Blood
Robin Blood

Reputation: 26

based on your comment, I think you don't need CMD you can start the 'mysqlcheck' directly using a different executable (see below)

to get all the results from the command-line can be a bit tricky. I added a working sample at the end of the answer but I'd like to explain it a bit.

set up what you want to execute first

        FileInfo executable = new FileInfo(@"C:\Temp\cmd.bat");

        ProcessStartInfo startInfo = new ProcessStartInfo(executable.FullName);
        startInfo.Arguments = "two arguments";
        startInfo.UseShellExecute = false;
        startInfo.CreateNoWindow = true;
        startInfo.RedirectStandardOutput = true;

make sure to redirect the output (also I don't want to see it) Now you give your StartInfo to the process

        Process process = new Process();
        process.StartInfo = startInfo;

because we want to read the Output we need to

        process.EnableRaisingEvents = true;

I assigned two events:

        process.OutputDataReceived += new ...
        process.Exited += new ...

start the process and start reading

        process.Start();
        process.BeginOutputReadLine();

The batchFile I used gave me the file and the arguments

@echo off
echo %~nx0
echo %1
echo %2

you can call any executable like this (except cmd.exe, it never finishes)

I attached the complete code and also added a Reset-Event to sync the execution. Here is the output from my debug-console:

  • Beginn
  • Starting external Application: C:\Temp\cmd.bat
  • cmd.bat
  • two
  • arguments
  • Finished executing external Application
  • End

listing:

    // using System.Diagnostics;
    // using System.Threading;
    // using System.IO

    private ManualResetEvent mre;

    private void button_Click(object sender, EventArgs e)
    {
        mre = new ManualResetEvent(false);
        Debug.WriteLine("Beginn");

        FileInfo executable = new FileInfo(@"C:\Temp\cmd.bat");

        ProcessStartInfo startInfo = new ProcessStartInfo(executable.FullName);
        startInfo.Arguments = "two arguments";
        startInfo.UseShellExecute = false;
        startInfo.CreateNoWindow = true;
        startInfo.RedirectStandardOutput = true;

        Process process = new Process();
        process.EnableRaisingEvents = true;
        process.StartInfo = startInfo;
        process.OutputDataReceived += new DataReceivedEventHandler(process_OutputDataReceived);
        process.Exited += new EventHandler(process_Exited);

        Debug.WriteLine(String.Format("Starting external Application: {0}", executable.FullName));
        process.Start();
        process.BeginOutputReadLine();

        mre.WaitOne();
        Debug.WriteLine("End");
    }

    void process_OutputDataReceived(object sender, DataReceivedEventArgs e)
    {
        Debug.WriteLine(e.Data);
    }

    void process_Exited(object sender, EventArgs e)
    {
        Debug.WriteLine("Finished executing external Application");
        mre.Set();
    }

hope it helps, best regards

Upvotes: 1

Magnetron
Magnetron

Reputation: 8573

You need to start strCmdText with /C:

static void Main(string[] args)
{
    string strCmdText = "/C mysqlcheck -r JAMFSOFTWARE -u root -p password";
    System.Diagnostics.Process.Start("CMD.exe", strCmdText);

    string strCmdText2 = "/C mysqlcheck -o JAMFSOFTWARE -u root -p password";
    System.Diagnostics.Process.Start("CMD.exe", strCmdText);

    string strCmdText3 = "/C mysqlcheck -c JAMFSOFTWARE -u root -p password";
    System.Diagnostics.Process.Start("CMD.exe", strCmdText);

}

Upvotes: 0

Related Questions