Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Problem running a .bat file from a C# forms application

Problem running a .bat file from a C# forms application

Scheduled Pinned Locked Moved C#
helpcsharp
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 4180484
    wrote on last edited by
    #1

    I have this code developed to run a bat file. the code is set to a buttonclick event......bat file is crating correctly ..when i double click on it it works fine..but the bat is not executing from the program..cant figure out what the reason..your help is appriciated..

    string text = "compdld -d -i\"C:\\softpay\\ver22\\output\\cpac\\ads\\files\\softpayx.dld\" -w\"C:\\HSBMFC\" -momni-5150 -aHSBMFC -tTID01 -gl -fl";
    File.WriteAllText(@"C:\COMPDLD\a.bat", text);
    ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo();
    psi.FileName = @"C:\COMPDLD\a.bat";
    psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
    System.Diagnostics.Process p = new System.Diagnostics.Process();
    p.StartInfo = psi;
    p.EnableRaisingEvents = true;
    p.Start();

            while (!p.HasExited)
            {
                System.Threading.Thread.Sleep(1000);
            }
            if (p.ExitCode != 0)
            {
                //some error occurred
            }
    
    F 1 Reply Last reply
    0
    • U User 4180484

      I have this code developed to run a bat file. the code is set to a buttonclick event......bat file is crating correctly ..when i double click on it it works fine..but the bat is not executing from the program..cant figure out what the reason..your help is appriciated..

      string text = "compdld -d -i\"C:\\softpay\\ver22\\output\\cpac\\ads\\files\\softpayx.dld\" -w\"C:\\HSBMFC\" -momni-5150 -aHSBMFC -tTID01 -gl -fl";
      File.WriteAllText(@"C:\COMPDLD\a.bat", text);
      ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo();
      psi.FileName = @"C:\COMPDLD\a.bat";
      psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
      System.Diagnostics.Process p = new System.Diagnostics.Process();
      p.StartInfo = psi;
      p.EnableRaisingEvents = true;
      p.Start();

              while (!p.HasExited)
              {
                  System.Threading.Thread.Sleep(1000);
              }
              if (p.ExitCode != 0)
              {
                  //some error occurred
              }
      
      F Offline
      F Offline
      Franck Paquier
      wrote on last edited by
      #2

      Good morning. May be you should also specify the working directory. or try to launch cmd.exe with your batch file as argument (same command as the one created when you make a shorcut on a batch file: C:\WINDOWS\system32\cmd.exe /k mybatch.bat. Regards.

      N realJSOPR 2 Replies Last reply
      0
      • F Franck Paquier

        Good morning. May be you should also specify the working directory. or try to launch cmd.exe with your batch file as argument (same command as the one created when you make a shorcut on a batch file: C:\WINDOWS\system32\cmd.exe /k mybatch.bat. Regards.

        N Offline
        N Offline
        Natza Mitzi
        wrote on last edited by
        #3

        Get the standard output stream from the process

        Natza Mitzi

        1 Reply Last reply
        0
        • F Franck Paquier

          Good morning. May be you should also specify the working directory. or try to launch cmd.exe with your batch file as argument (same command as the one created when you make a shorcut on a batch file: C:\WINDOWS\system32\cmd.exe /k mybatch.bat. Regards.

          realJSOPR Offline
          realJSOPR Offline
          realJSOP
          wrote on last edited by
          #4

          You actually should do both (specify the correct working folder and using cmd.exe).

          "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
          -----
          "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups