batch-filecmdpingipconfig

Batch-file for running commands in cmd


I'm working on a little fun school project.

I want to create a windows form application, which runs commands in cmd by opening batch-file. I do not have much experience in using batch-files.

I need help with the following:

  1. Batch file which opens cmd
  2. Runs a command ONCE only, such as ipconfig, ping, tracert etc.
  3. Keeps cmd open with the information

Otherwise good guides for working and learning batch-files are much appreciated.


Solution

  • I'll give you some hints:

    1. Batch files open with cmd by default
    2. Commands will only run once by default
    3. To keep the window open after running the command, use the /k switch i.e. cmd /k ipconfig

    EDIT:

    Make this the contents of your batch file:

    @echo off
    ipconfig
    

    Then call the batch file like this (from command line/run etc):

    cmd /k iptest.bat
    

    Or from Winform (C#):

    Process p = new Process();
    p.StartInfo.FileName = "cmd.exe";
    p.StartInfo.Arguments = "/k iptest.bat"
    p.Start();