C# Tutorials and offshore development in India
    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



Active Members
TodayLast 7 Days 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: sriluMember 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  
(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



dotNet Slackers   BizTalk Adaptors    Web Design


Contact Us    Privacy Policy    Terms Of Use