Wednesday, August 29, 2012

XML node


using System; using System.IO; using System.Xml; public class Sample
{
public static void Main()
{

XmlDocument doc = new XmlDocument();
doc.LoadXml("" +
"Pride And Prejudice" +
"
");
XmlNode root = doc.DocumentElement;
//Create a new node.
XmlElement elem = doc.CreateElement("price");
elem.InnerText="19.95";
//Add the node to the document.
root.AppendChild(elem);
Console.WriteLine("Display the modified XML...");
doc.Save(Console.Out);
}
}
http://msdn.microsoft.com/en-us/library/system.xml.xmlnode.appendchild.aspx

Monday, August 27, 2012

Create, Read, Write, Copy, Move and Delete a Text File using C#


private void btnCreate_Click(object sender, EventArgs e)
{
FileStream fs = null;
if (!File.Exists(fileLoc))
{
using (fs = File.Create(fileLoc))
{
}
}
}
http://www.dotnetcurry.com/ShowArticle.aspx?ID=144

Get Size of file on disk C#


fileName = @"C:\Program Files\CLIRetail.log";
FileInfo f = new FileInfo(fileName);
long s1 = f.Length;
string size = GetFileSize(s1);
Console.WriteLine("Size : " + size);


public static string GetFileSize(long Bytes) {
if (Bytes >= 1073741824) {
Decimal size = Decimal.Divide(Bytes, 1073741824);
return String.Format("{0:##.##} GB", size);
}
else if (Bytes >= 1048576)
{
Decimal size = Decimal.Divide(Bytes, 1048576);
return String.Format("{0:##.##} MB", size);
}
else if (Bytes >= 1024)
{
Decimal size = Decimal.Divide(Bytes, 1024);
return String.Format("{0:##.##} KB", size);
}
else if (Bytes > 0 & Bytes < 1024)
{
Decimal size = Bytes;
return String.Format("{0:##.##} Bytes", size);
}
else
{
return "0 Bytes";
}