[C#]How To End A Process

Ugh, here...

Code:
private void CloseBtn_Click(object sender, EventArgs e)
        {
            foreach (Process XZeenonProcess in Process.GetProcesses())
            {
                if (XZeenonProcess.ProcessName.StartsWith(NOTEPAD))
                {
                    XZeenonProcess.Kill();
                }
            }
        }

where it says NOTEPAD change that to w/e process u want to kill but do not add .exe at the end. Just the name.

Credits to me, oviously lol

Says:
The name "NOTEPAD" does not exist in the current context.
 
It's still the same thing.
Bat file stops processing, but it stays there.

Code:
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace ServerRestart
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        Process startServer = new Process();

        private void helpToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Step 1.  Name your bat files.  In the provided textboxes.  Input the name of the bat files.  (e.g If your first bat to be             launched is world.bat, put world.bat");
            MessageBox.Show("OPTIONAL: State the time you want in between bat files launched.");
            MessageBox.Show("Step 2. Click the 'Start Server' button. ");
            MessageBox.Show("If at anytime you want to shut down, click the 'Close Server' button.  To restart, click the 'Restart Server' button.");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            richTextBox1.Text = "";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            startServer.StartInfo.FileName = "serverstart.bat";
            startServer.Start();
            richTextBox1.Text = richTextBox1.Text + DateTime.Today.ToString("MM/dd/yyyy") + (" - All instances of server are running. (Java)\r\n");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            foreach (Process startServer in Process.GetProcesses())
            {
                if (startServer.ProcessName.StartsWith("cmd"))//Tried with serverstart too
                {
                    startServer.Kill();
                }
            }


            richTextBox1.Text = richTextBox1.Text + DateTime.Today.ToString("MM/dd/yyyy") + (" - All instaces of server have been stopped. (Java)\r\n");
        }

        private void button4_Click(object sender, EventArgs e)
        {
            startServer.StartInfo.FileName = "serverstart.bat";
            startServer.Kill();
            richTextBox1.Text = richTextBox1.Text + DateTime.Today.ToString("MM/dd/yyyy") + (" - All instaces of server have been stopped. (Java)\r\n");
            startServer.Start();
            richTextBox1.Text = richTextBox1.Text + DateTime.Today.ToString("MM/dd/yyyy") + (" - All instances of server are running. (Java)\r\n");
        }

        private void button5_Click(object sender, EventArgs e)
        {
            startServer.StartInfo.FileName = "Maplestoryserver.exe";
            startServer.Start();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            startServer.StartInfo.FileName = "Maplestoryserver.exe";
            startServer.Kill();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            startServer.StartInfo.FileName = "Maplestoryserver.exe";
            startServer.Kill();
            startServer.Start();
        }

        private void setAutoRestartToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form2 restarttime = new Form2();
            restarttime.Show();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            startServer.Kill();
            richTextBox1.Text = richTextBox1.Text + DateTime.Today.ToString("MM/dd/yyyy") + (" - All instaces of server have been stopped. (Java)\r\n");
            startServer.StartInfo.FileName = "serverstart.bat";
            startServer.Start();
            richTextBox1.Text = richTextBox1.Text + DateTime.Today.ToString("MM/dd/yyyy") + (" - All instances of server are running. (Java)\r\n");
        }

        private void button8_Click(object sender, EventArgs e)
        {
            timer1.Interval = int.Parse(textBox1.Text);
            timer1.Start();

        }

        private void creditsToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Form2 credits = new Form2();
            credits.Show();
        }

        private void visitSoyDEVToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Process.Start("http://soy.no-ip.biz/");
        }

        private void calculatorToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Process.Start("calc.exe");
        }

        private void tabPage1_Click(object sender, EventArgs e)
        {

        }
    }
}
 
As bounty hunter said .bat files are not programs. So it really isn't a process.

Why dont u try put in the command of the .bat file inside ur program?

For example:

Process BatFile = New BatFile();
BatFile.StartInfo.FileName = "cmd.exe";
BatFile.StartInfo.Arguments = "What ever is inside the bat file.";
BatFile.Start();

Etc.
 
But that just opens up cmd.exe

Code:
        private void button2_Click(object sender, EventArgs e)
        {
            Process startServer = new Process();

            startServer.StartInfo.FileName = "cmd.exe";
            startServer.StartInfo.Arguments = "@echo off\r\ntitle serverstart\r\ncolor 1b\r\ntitle World\r\nstart /b launch_world.bat\r\ntitle serverstart\r\nping localhost -w 100 >nul\r\nstart /b launch_login.bat\r\rtitle serverstart\r\nping localhost -w 100 >nul\r\nstart /b launch_channel.bat\r\ntitle serverstart\r\nping localhost -w 100 >nul\r\ncolor b\r\ntitle serverstart";

            startServer.Start();


            richTextBox1.Text = richTextBox1.Text + DateTime.Today.ToString("MM/dd/yyyy") + (" - All instaces of server have been stopped. (Java)\r\n");
        }

I'm pretty sure it's because I did the Arguments part incorrectly?
 
But that just opens up cmd.exe

Code:
        private void button2_Click(object sender, EventArgs e)
        {
            Process startServer = new Process();

            startServer.StartInfo.FileName = "cmd.exe";
            startServer.StartInfo.Arguments = "@echo off\r\ntitle serverstart\r\ncolor 1b\r\ntitle World\r\nstart /b launch_world.bat\r\ntitle serverstart\r\nping localhost -w 100 >nul\r\nstart /b launch_login.bat\r\rtitle serverstart\r\nping localhost -w 100 >nul\r\nstart /b launch_channel.bat\r\ntitle serverstart\r\nping localhost -w 100 >nul\r\ncolor b\r\ntitle serverstart";

            startServer.Start();


            richTextBox1.Text = richTextBox1.Text + DateTime.Today.ToString("MM/dd/yyyy") + (" - All instaces of server have been stopped. (Java)\r\n");
        }

I'm pretty sure it's because I did the Arguments part incorrectly?



try remove: startServer.StartInfo.FileName = "cmd.exe";
 
InvalidOperationException was unhandled.

Code:
        private void button2_Click(object sender, EventArgs e)
        {
            Process startServer = new Process();

            //startServer.StartInfo.FileName = "cmd.exe";
            startServer.StartInfo.Arguments = "@echo off\r\ntitle serverstart\r\ncolor 1b\r\ntitle World\r\nstart /b launch_world.bat\r\ntitle serverstart\r\nping localhost -w 100 >nul\r\nstart /b launch_login.bat\r\rtitle serverstart\r\nping localhost -w 100 >nul\r\nstart /b launch_channel.bat\r\ntitle serverstart\r\nping localhost -w 100 >nul\r\ncolor b\r\ntitle serverstart";

            startServer.Start();


            richTextBox1.Text = richTextBox1.Text + DateTime.Today.ToString("MM/dd/yyyy") + (" - All instaces of server have been stopped. (Java)\r\n");
        }
 
InvalidOperationException was unhandled.

Code:
        private void button2_Click(object sender, EventArgs e)
        {
            Process startServer = new Process();

            //startServer.StartInfo.FileName = "cmd.exe";
            startServer.StartInfo.Arguments = "@echo off\r\ntitle serverstart\r\ncolor 1b\r\ntitle World\r\nstart /b launch_world.bat\r\ntitle serverstart\r\nping localhost -w 100 >nul\r\nstart /b launch_login.bat\r\rtitle serverstart\r\nping localhost -w 100 >nul\r\nstart /b launch_channel.bat\r\ntitle serverstart\r\nping localhost -w 100 >nul\r\ncolor b\r\ntitle serverstart";

            startServer.Start();


            richTextBox1.Text = richTextBox1.Text + DateTime.Today.ToString("MM/dd/yyyy") + (" - All instaces of server have been stopped. (Java)\r\n");
        }

Idk sorry. I dont hasve your files to test it out.
 
Batch files are sequences of commands.

Code:
public static int ProcessBatchFile()
{
    //Create a variable to hold the value you wish
    //to return to your calling method
    int batchSuccessful;
    //Create a new Process(), I just happen
    //to name this "YourName"
    Process batchExecute = new Process();

    //This is a method that you can use to set
    //properties for your new Process()
    ProcessStartInfo batchExecuteInfo = new ProcessStartInfo();

    //Tell the program to not use the built-in Shell provided
    //with the OS, wew ant to start it ourselved
    batchExecuteInfo.UseShellExecute = false;

    //Tell the process to not open a Console window
    batchExecuteInfo.CreateNoWindow = true;

    //Set your process info here
    batchExecute.StartInfo = batchExecuteInfo;
    //Start your Process(), this will start the
    //command line and send your command
    batchExecute.Start("Path to your batch file here");

    //Set the period of time to wait for the associated
    //process to exit, and blocks the current thread
    //until the time has elapsed or the
    //process has exited
    batchExecute.WaitForExit(15000);

    //Create a variable and assign it the value
    //of your processes exit code
    int ExitProcess = batchExecute.ExitCode;

    //Check if the process has completed
    //and return a status of 0 (succeeded). If not
    //then we want to kill the process
    if (ExitProcess > 0 & !batchExecute.HasExited)
    {
        //Here you can assign a value to a variable
        //to return to your calling method
        batchSuccessful = 1;
        batchExecute.Kill();
    }
    else
    {
        //Do something here and
        //assign your successful variable a 0
        batchSuccessful = 0;
    }
    //Return the value (1 or 0) to
    //your calling method
    return batchSuccessful;
}
http://www.dreamincode.net/forums/showtopic35582.htm
 
Back