Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: execute a .cmd file using C#  (Read 12352 times)

0 Members and 1 Guest are viewing this topic.

jet ved

    Topic Starter


    Greenhorn

    execute a .cmd file using C#
    « on: December 17, 2008, 08:41:37 AM »
    Hi,
    Can someone help me with the script for running a .cmd file using C# code. I know that there is a 'process' object which comes into picture.

    Thanks,
    jet.

    hibyy



      Rookie

      • Experience: Familiar
      • OS: Windows 8
      Re: execute a .cmd file using C#
      « Reply #1 on: August 24, 2009, 01:07:36 PM »
      you know you can do more then just make a c# file run a cmd file but you can also make it do  cmd commands.
      EXAMPLE:
      Code: [Select]
      using System.Runtime.InteropServices;
      namespace Run
      {
      public class CMD
      {
      [DllImport("msvcrt.dll")]
      public static extern int system(string cmd);
      public static void Main()
      {
      system("Echo Hello");
      system("pause");
      }
      }
      }

      If this helped please tell me ^^

      BC_Programmer


        Mastermind
      • Typing is no substitute for thinking.
      • Thanked: 1140
        • Yes
        • Yes
        • BC-Programming.com
      • Certifications: List
      • Computer: Specs
      • Experience: Beginner
      • OS: Windows 11
      Re: execute a .cmd file using C#
      « Reply #2 on: August 24, 2009, 02:01:49 PM »
      Code: [Select]
              static void Main(string[] args)
              {
                  System.Diagnostics.Process useprocess;
       
                  useprocess = new System.Diagnostics.Process();
                  useprocess.StartInfo.FileName="cmd.exe";
                  useprocess.Start();

                 
                         
              }

      change the useprocess.StartInfo.FileName= line to reflect the batch file you want to run. :)
      I was trying to dereference Null Pointers before it was cool.