Brandon Printiss
Brandon Printiss

Reputation: 111

Need help launching command in cmd through C#

I have a .bat file that launches a command, I would like to do away with using a .bat file and programmatically start the process in C#.

Here is the .bat command line

start ShooterGameServer.exe TheIsland?QueryPort=27015?SessionName=ARKServer?MaxPlayers=5?listen?ServerPassword=55555?ServerAdminPassword=55555 -nosteamclient -game -server -log

I tried setting it up like this in C#

Process.Start("CMD.exe", string.Format("Start {0} TheIsland?QueryPort=27015?SessionName?{1}?MaxPlayers={3}?listen?ServerPassword={2}?ServerAdminPassword={2} -nosteamclient -game -server -log", ArkServer.FileName, textBox1.Text, textBox2.Text, numericUpDown1.Value.ToString()));

All I end up with after running that command in C# is a cmd window with this printed on it

C:\Users\*******\Documents\Visual Studio 2013\Projects\ArkProfileEditor\ArkProfi
leEditor\bin\Debug>

Upvotes: 0

Views: 240

Answers (3)

Brandon Printiss
Brandon Printiss

Reputation: 111

            SaveFileDialog saveserver = new SaveFileDialog();
        saveserver.FileName = "ARKServerStart.bat";
        saveserver.Filter = "ARKServerStart (*.bat)|*.bat";
        saveserver.InitialDirectory = ArkServer.FileName;
        if (saveserver.ShowDialog() == DialogResult.OK)
        {
            StreamWriter SW = new StreamWriter(saveserver.FileName);
            string runit = string.Format("start ShooterGameServer.exe TheIsland?QueryPort=27015?SessionName={0}?MaxPlayers={3}?listen?ServerPassword={1}?ServerAdminPassword={2} -nosteamclient -game -server -log", ServName.Text, ServPass.Text, AdmnPass.Text, MPlayers.Value.ToString(), ArkProfile.FileName.Replace("ShooterGameServer.exe", ""));
            SW.WriteLine(runit);
            SW.Close();
            var dir = new ProcessStartInfo();
            Path.GetDirectoryName(saveserver.FileName);
            dir.WorkingDirectory = Path.GetDirectoryName(saveserver.FileName);
            dir.FileName = saveserver.FileName;
            dir.CreateNoWindow = true;
            Process pro = Process.Start(dir);
            Clipboard.SetText(saveserver.FileName);
        }

This is the code I ended up with for the final product :) it works great! Hopefully it helps someone else who needs it.

Upvotes: 1

Phillip Scott Givens
Phillip Scott Givens

Reputation: 5464

I think what you meant to do was launch the exe and not the command window. Try the following

Process.Start("ShooterGameServer.exe", string.Format("TheIsland?QueryPort=27015?SessionName?{1}?MaxPlayers={3}?listen?ServerPassword={2}?ServerAdminPassword={2} -nosteamclient -game -server -log", ArkServer.FileName, textBox1.Text, textBox2.Text, numericUpDown1.Value.ToString()));

Make sure that ShooterGameServer.exe is in the same directory as the exe launching it or pass an appropriate path.

Upvotes: 1

SLaks
SLaks

Reputation: 887215

cmd.exe does not take arguments like that.

You want to run cmd /c ... to tell cmd to run that command.

Upvotes: 0

Related Questions