Search This Blog

Monday, February 16, 2015

Select DataTable rows with where LINQ C#

C# > LINQ > Where

Where filters a sequence of values based on a predicate. 

Example

Select DataTable rows with where.


DataTable dt = new DataTable();
dt.Columns.Add("name", typeof(string));
dt.Rows.Add("john");
dt.Rows.Add("dan");

var filteredRows = dt.Rows
                  .Cast<DataRow>()
                  .Where(r => r["name"] == "dan").ToList();




Wednesday, February 11, 2015

Create new directory with DirectoryInfo C#

C# > Files > DirectoryInfo Class

Use this class for create, move and enumerating directories and subdirectories. 

Example 

Create new directory

DirectoryInfo directoryInfo = new DirectoryInfo(@"c:\Yourdir");
try
{
if (!directoryInfo.Exists)
       {
              directoryInfo.Create();
       }
}
catch (Exception ex)
{
MessageBox.Show("DirectoryInfo error: {0}", ex.ToString());
}
finally { }





Monday, February 2, 2015

Get Directory Size C#

C# > IO  > FileInfo > Length

Get the size of the file in bytes. 

Example

Get Directory Size C#


public long DirectorySize(DirectoryInfo dir)
{
       long size = 0;
       foreach (FileInfo file in dir.GetFiles())
       {
              size += file.Length;
       }
      foreach (DirectoryInfo directory in dir.GetDirectories())
       {
              size += DirectorySize(directory);
       }
       return size;
 }

DirectoryInfo dir = new DirectoryInfo(@"C:\Temp");
long size = DirectorySize(dir);








Monday, January 19, 2015

C# Capture Screen to PictureBox

C# > Drawing > Capture Screen

Bitmap printscreen = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
Graphics graphics = Graphics.FromImage(printscreen as Image);
graphics.CopyFromScreen(0, 0, 0, 0, printscreen.Size);
using (MemoryStream s = new MemoryStream())
{
printscreen.Save(s, System.Drawing.Imaging.ImageFormat.Bmp);
       picCapture.Size = new System.Drawing.Size(this.Width, this.Height);
       picCapture.Image = Image.FromStream(s);

}





XmlDocument C# Example

C# > XML > XmlDocument

Represents an XML document.

Example


XmlDocument doc = new XmlDocument();
doc.Load(xmlfile);




NetworkChange Class C#

C# > System.Net > NetworkInformation > NetworkChange

Use this class to receive notification when the IP address of a network interface changes.
Changes can appears when:

  • disconnect network cable
  • out of range of a wireless Local Area Network
  • hardware failure
Events:





    C# NetworkInformation NameSpace

    C# > System.Net > NetworkInformation


    Provides access to network address information and traffic data.

    Classes