Running a batch file from an ASP.Net page.

Discussion in 'ASP .Net' started by UJ, Sep 23, 2005.

  1. UJ

    UJ Guest

    I know I know - I've already posted this in the DOTNET.general area but my
    problem seems to be specific to asp.net so I'll recap here......

    I (after much work) got to the point where I have a batch file that will do
    some commands for me. I can run it from a DOS box, double click on it from
    Window's Explorer and can run it from a VB.Net console app. The code for the
    console app is here:

    Dim p As System.Diagnostics.Process = New System.Diagnostics.Process
    Try
    p.StartInfo.RedirectStandardOutput = False
    p.StartInfo.RedirectStandardError = False
    p.StartInfo.UseShellExecute = True
    p.StartInfo.FileName =
    "C:\MarlinApplication\Contents\Items\792\LeftPanel.bat"
    p.Start()
    p.WaitForExit()
    Catch ex As Exception
    Dim lstemp As String
    lstemp = ex.Message
    Dim t As String
    t = lstemp
    End Try

    The batch file runs some stuff and creates an output file.

    This code works just ducky. When I put the exact same code in ASP.Net I get
    nothing. No errors, no output file, nothings. Looks like it's not even
    running (the WaitForExit returns to quickly.....).

    Anybody got any thoughts?

    TIA - Jeff
    UJ, Sep 23, 2005
    #1
    1. Advertising

  2. UJ

    Lenard Gunda Guest

    Hi,

    ASP.NET applications run under the ASPNET account. Maybe that account does
    not have enough rights (probably does not by default) to run your file? Or
    maybe the .BAT file is run, but what is inside, that cannot run (because
    your .BAT is ran with ASPNET account, so are what is inside it).


    -Lenard


    "UJ" <> wrote in message
    news:...
    >I know I know - I've already posted this in the DOTNET.general area but my
    >problem seems to be specific to asp.net so I'll recap here......
    >
    > I (after much work) got to the point where I have a batch file that will
    > do some commands for me. I can run it from a DOS box, double click on it
    > from Window's Explorer and can run it from a VB.Net console app. The code
    > for the console app is here:
    >
    > Dim p As System.Diagnostics.Process = New System.Diagnostics.Process
    > Try
    > p.StartInfo.RedirectStandardOutput = False
    > p.StartInfo.RedirectStandardError = False
    > p.StartInfo.UseShellExecute = True
    > p.StartInfo.FileName =
    > "C:\MarlinApplication\Contents\Items\792\LeftPanel.bat"
    > p.Start()
    > p.WaitForExit()
    > Catch ex As Exception
    > Dim lstemp As String
    > lstemp = ex.Message
    > Dim t As String
    > t = lstemp
    > End Try
    >
    > The batch file runs some stuff and creates an output file.
    >
    > This code works just ducky. When I put the exact same code in ASP.Net I
    > get nothing. No errors, no output file, nothings. Looks like it's not even
    > running (the WaitForExit returns to quickly.....).
    >
    > Anybody got any thoughts?
    >
    > TIA - Jeff
    >
    >
    Lenard Gunda, Sep 23, 2005
    #2
    1. Advertising

  3. UJ

    Acheliz

    Joined:
    Jun 23, 2008
    Messages:
    1
    Finally I found it

    Hi frenz,

    Here's the code to execute batch file from ASP.net.The code is simple.
    Enjoy!!!

    Partial Class _Default
    Inherits System.Web.UI.Page
    //FILEPATH
    Dim strFilePath As String = "d:\\Gautam\\Runme.bat"
    Protected Sub cmdExecute_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdExecute.Click
    //START CMD
    Dim psi As New System.Diagnostics.ProcessStartInfo("cmd.exe")
    psi.UseShellExecute = False
    psi.RedirectStandardInput = True
    psi.RedirectStandardOutput = True
    psi.RedirectStandardError = True
    psi.WorkingDirectory = "d:\\Gautam\\"
    //DECLARE PROCESS
    Dim proc As System.Diagnostics.Process
    proc = System.Diagnostics.Process.Start(psi)
    //PATH OPENED AS TEXT BY STREAM READER
    Dim strm As System.IO.StreamReader
    strm = System.IO.File.OpenText(strFilePath)
    //OUTPUT
    Dim sout As System.IO.StreamReader
    sout = proc.StandardOutput
    //INPUT
    Dim sin As System.IO.StreamWriter
    sin = proc.StandardInput
    //COMMAND TO CMD
    While (strm.Peek() <> -1)
    sin.WriteLine(strm.ReadLine())
    End While
    strm.Close()
    Dim stEchoFmt As String
    stEchoFmt = "# {0} run successfully. Exiting"
    sin.WriteLine(String.Format(stEchoFmt, strFilePath))
    sin.WriteLine("EXIT")
    proc.Close()
    //READ THE EXECUTED BATCH FILE
    Dim results As String
    results = sout.ReadToEnd.Trim
    //CLOSE IN/OUT
    sin.Close()
    sout.Close()
    //WORKS DONE JUST FORMAT THE CHARACTERS TO DISPLAY
    Dim fmtStdOut As String
    fmtStdOut = "<font face=courier size=0>{0}</font>"
    Response.Write(String.Format(fmtStdOut, results.Replace(System.Environment.NewLine, "<br>")))
    End Sub
    End Class
    Acheliz, Jun 23, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Kraft

    running dos batch file

    John Kraft, Feb 3, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    627
    Brian W
    Feb 3, 2004
  2. John McGinty

    Running batch script from ASP.NET page

    John McGinty, Sep 28, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    763
    John McGinty
    Sep 28, 2004
  3. Stephen

    Running Batch Files using ASP .NET

    Stephen, Dec 21, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    2,550
    Stephen
    Dec 22, 2005
  4. David Hubbard
    Replies:
    0
    Views:
    455
    David Hubbard
    Jan 24, 2006
  5. Doug at SAU

    running a batch file

    Doug at SAU, Jun 20, 2005, in forum: ASP General
    Replies:
    9
    Views:
    360
    Chris Hohmann
    Jun 21, 2005
Loading...

Share This Page