Running a batch file from an ASP.Net page.

U

UJ

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
 
L

Lenard Gunda

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
 
Joined
Jun 23, 2008
Messages
1
Reaction score
0
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top