Reply to topic
How to Execute a .bat file in .NET
Jason101
Forum Regular

Joined: 14 Mar 2006
Posts: 548
Location: Harrisburg, PA
Reply with quote
OK, heres the deal. I don't know a thing about .net. I searched the net for a few hours, and couldn't find what what I needed.

Here's what I'm trying to accomplish.

I want to execute a .bat file on my server that will restart the ColdFusion 8 service. The bat file is created and works. So that's not the issue. I just need help executing it with.Net. (I could have used <cfexecute> in cf8, but if the ColdFusion service is down what good does that do me) Rolling Eyes

I found this snippet on a blog that will do what I need it to (I think)

http://axdimensions.wordpress.com/2008/02/07/run-a-bat-file-from-net-with-parameters/

But I have no clue how to call the page, function or anything.

Would someone who is .net savvy mind getting me started with a simple page? I'd hope to return the favor if that person ever needs CF help Very Happy

Oh yeah.. I'm not on a Shared Server so no worries about execute permissions.
I'm running Server 2008 & IIS7

Thanks in advance!
rcorbin


Joined: 02 Jul 2007
Posts: 65
Location: Newark, DE
Reply with quote
does it have to be .net? I know you can do it with regular ASP...I've done it with something like

Code:
<%
   Response.Write ("Starting some batch files…")
   Set oScript = Server.CreateObject("WSCRIPT.SHELL")
   Call oScript.Run ("cmd.exe /c C:\websites\bat.bat > c:\websites\mylog.log")
   Set oScript = Nothing
   Response.Write "ok."
%>



Though there are some permissions that need changed...I think the default app pool wont have permission..I think i had to change it to 'local system' and made sure the web entry could run scripts and executables? Something like that...let me know if you cant get it working. This is for 2003 and iis 6.0 Smile

-Ray
How about this?
jholbrook


Joined: 12 May 2008
Posts: 1
Reply with quote
You could use System.Diagnostics.Process and call the command you desire directly, however you will need to ensure that the account your application is running as has permission to do so, and also consider the security implications of elevating the account. Here is a simple example of calling a shell command from the code-behind.

'Declarations
Imports System.Diagnostics.Process

Dim p As Diagnostics.Process = New Diagnostics.Process
p.StartInfo.FileName = "C:\Windows\System32\cmd.exe" ' this is the name of the process we want to execute
p.StartInfo.Arguments = "/C net start ""Coldfusion 8 Application Server""" 'Arguments to pass to the shell
p.StartInfo.UseShellExecute = False 'Used to allow us to get output below
p.StartInfo.RedirectStandardOutput = True 'Used to allow us to get output below
p.Start() ' start the process
Dim output As String = p.StandardOutput.ReadToEnd 'get output
p.WaitForExit() 'wait for completion


This is in VB.net you can convert it here if you use C#:
http://labs.developerfusion.co.uk/convert/csharp-to-vb.aspx

Let me know how it works out, or if you need another example. Smile
How to Execute a .bat file in .NET
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
All times are GMT  
Page 1 of 1  

  
  
 Reply to topic