Tutorials
Resources
Forum
Communities
Interview
Jobs
Projects
Offshore Development
Silverlight Tutorials
|
Mentor
|
Code Converter
|
Articles
|
Code Factory
|
Computer Jokes
|
Members
|
Peer Appraisal
|
IT Companies
|
Bookmarks
|
Revenue Sharing
|
Prizes & Awards
My Profile
Sign In
Register
AdSense Revenue
Active Members
Today
Appukuttan
(94)
Abhi
(72)
Babu Akkandi
(63)
Last 7 Days
Appukuttan
(619)
Babu Akkandi
(470)
divya
(321)
more...
New Feature:
Community Sites
:
Create your own .NET community website and start earning from Google AdSense !
It's Free !
Creating and Executing Batch Files Progrmatically using C#
Posted Date: 07 May 2008
Resource Type:
Code Snippets
Category:
ASP.NET WebForms
Author:
srilu
Member Level:
Diamond
Rating:
Points
: 20
This code helps you to create a batch file and also execute it programatically through the code in C#.
//create a batch file
string val = "test.bat";
FileInfo fi = new FileInfo(Server.MapPath(val));
StreamWriter sw = fi.CreateText();
//The List Of Operations you want to execute.
sw.WriteLine("C:\\Inetpub\\wwwroot\\postinfo.html");
sw.WriteLine("D:\\myfile.xml");
sw.WriteLine("C:\\WINDOWS\\Microsoft.NET\\Framework\\v2.0.50727\\aspnet_regsql");
sw.Close();
//execute the batch file
string strFilePath = Server.MapPath("test.bat");
System.Diagnostics.ProcessStartInfo procinfo = new ProcessStartInfo("cmd.exe");
procinfo.UseShellExecute = false;
procinfo.RedirectStandardError = true;
procinfo.RedirectStandardInput = true;
procinfo.RedirectStandardOutput = true;
System.Diagnostics.Process process = System.Diagnostics.Process.Start(procinfo);
System.IO.StreamReader stream = System.IO.File.OpenText(strFilePath);
System.IO.StreamReader sroutput = process.StandardOutput;
System.IO.StreamWriter srinput = process.StandardInput;
while (stream.Peek() != -1)
{
srinput.WriteLine(stream.ReadLine());
}
stream.Close();
string Echo="# {0} run successfully. Exiting";
srinput.WriteLine(string.Format(Echo, strFilePath));
srinput.WriteLine("EXIT");
process.Close();
string results = sroutput.ReadToEnd().Trim();
srinput.Close();
sroutput.Close();
string fmtOutput = "
{0}
";
this.Response.Write(String.Format(fmtOutput,results.Replace(System.Environment.NewLine, "
")));
Responses
No responses found. Be the first to respond and make money from
revenue sharing program
.
Feedbacks
Popular Tags
What are tags ?
Search Tags
Sign In
(No tags found.)
Post Feedback
This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must
Sign In
to post a response.
Next Resource:
How to display a Message Box in C#
Previous Resource:
Sending mail using gmail smtp server
Return to Discussion Resource Index
Post New Resource
Category:
ASP.NET WebForms
Post resources and
earn money
!
Related Resources
Adding Controls Dynamically in ASP.NET
Get all Session keys
Creating a Plugin.xml file while creating custom webparts
Image Preview (Javascript and DataRepeater)
how to use IF block in ASP.NET
dotNet Slackers
BizTalk Adaptors
Web Design
Contact Us
Privacy Policy
Terms Of Use