Running Batch Script from C#

Here is another example code to run batch script from C#


public void RunBatchScript(string batchPath, string arguments)
{
Process process = new Process();
process.StartInfo.FileName = batchPath;
process.StartInfo.Arguments = arguments;

process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardOutput = true;


process.OutputDataReceived += new DataReceivedEventHandler(writeStdOutStreamInfo);
process.ErrorDataReceived += new DataReceivedEventHandler(writeStdErrStreamInfo);

process.Start();

process.BeginErrorReadLine();
process.BeginOutputReadLine();

// Wait for the process to end
while (!process.HasExited)
{
Thread.Sleep(500); // sleep
}
Console.ReadKey();
}
public void writeStdOutStreamInfo(object sender,DataReceivedEventArgs e)
{
string s = e.Data;
Console.WriteLine(s);
}
public void writeStdErrStreamInfo(object sender, DataReceivedEventArgs e)
{
string s = e.Data;
Console.WriteLine(s);
}

Using there event handlers we will get the standardoutput and standarderror asynchronously in the command prompt

LinQ to XML

Using LinQ to XML its very easy to query the Xml files and traverse through the nodes and elements.
Here is one simple exaple to read the configuration XML file

bellow is my simple XML configuration file



CPADCE008
CPADCE008VM1
CPADCE044
CPADCE044VM4
20100504



Then here is the simple class to load the XML file

class ConfigElements
{
public string ServerMain { get; set; }
public string ServerVM { get; set; }
public string ClientMain { get; set; }
public string ClientVM { get; set; }
public string LatestBuild { get; set; }

public ConfigElements(XElement xElement)
{
ServerMain = xElement.Element("ServerMain").Value;
ServerVM = xElement.Element("ServerVM").Value;
ClientMain = xElement.Element("ClientMain").Value;
ClientVM = xElement.Element("ClientVM").Value;
LatestBuild = xElement.Element("LatestBuild").Value;
}

public ConfigElements(string ID)
{
XDocument doc = XDocument.Load("XMLFile2.xml");

var query = from xElem in doc.Descendants("Automation")
where xElem.Attribute("ID").Value ==ID
select new ConfigElements(xElem);

}
}


By creating the object of this class we will get all the reqired nodes

ConfigElements objConfigElements = new ConfigElements("1");
//pass here the Id of the requied values
string s= objConfigElements.ServerMain;
string t = objConfigElements.ServerVM;
string u = objConfigElements.ClientMain;
string v = objConfigElements.ClientVM;
string w = objConfigElements.LatestBuild;