September 25, 2010

List files and pictures in directories using C#

It is quite simple to list all files in a directory in C# using the System.IO.Directory class. This class contains the GetFiles(string path) method that returns all filenames as a string array within the path specified. It also contains the overloaded GetFiles(string path, string match) method that returns all filenames in a string array in the path and criteria (match) specified.

In the following example, I have demonstrated how to get all files in a directory and how to get all pictures from a particular directory.

Code Snippet
  1. public class DirectoryListing
  2. {
  3.     string path = @"C:\Users\shahed.kazi\Desktop";
  4.  
  5.     //Get all files in desktop
  6.     public string[] GetAllFiles()
  7.     {
  8.         return Directory.GetFiles(path);
  9.     }
  10.  
  11.     //Get pictures from desktop
  12.     public string[] GetAllPictures()
  13.     {
  14.         List<string[]> list = new List<string[]>();
  15.         list.Add(Directory.GetFiles(path, "*.bmp"));
  16.         list.Add(Directory.GetFiles(path, "*.png"));
  17.         list.Add(Directory.GetFiles(path, "*.gif"));
  18.         list.Add(Directory.GetFiles(path, "*.jpg"));
  19.         list.Add(Directory.GetFiles(path, "*.jpeg"));
  20.  
  21.  
  22.         List<string> filename = new List<string>();
  23.  
  24.         for (int i = 0; i < list.Count; i++)
  25.         {
  26.             foreach (string s in list[i])
  27.                 filename.Add(s);
  28.         }
  29.         return filename.ToArray();
  30.     }
  31. }

September 23, 2010

Get Drive Information in Windows Using C#

It is easy to get the drive information using C#. Information like available free space, name and volume label of the drive, total size of the drive can be easily obtained. Below is a sample code to demonstrate how the drive information can be obtained. System.IO.DriveInfo class has been used to retrieve drive information. Please note that the code is written within a try-catch block since exceptions may arise while using IO.

Code Snippet
  1. foreach (DriveInfo di in DriveInfo.GetDrives())
  2.             {
  3.                 string br = "<br/>";
  4.                 try
  5.                 {
  6.                     Response.Write("available free space: " + di.AvailableFreeSpace + br);
  7.                     Response.Write("drive format: " + di.DriveFormat + br);
  8.                     Response.Write("drive type: " + di.DriveType + br);
  9.                     Response.Write("is ready: " + di.IsReady + br);
  10.                     Response.Write("name: " + di.Name + br);
  11.                     Response.Write("root directory: " + di.RootDirectory + br);
  12.                     Response.Write("total free space: " + di.TotalFreeSpace + br);
  13.                     Response.Write("total size: " + di.TotalSize + br);
  14.                     Response.Write("volume label: " + di.VolumeLabel + br);
  15.                 }
  16.                 catch (Exception ex)
  17.                 {
  18.                     Response.Write(ex.Message);
  19.                 }
  20.             }

September 20, 2010

Asp.Net security vulnerability

Microsoft has reported a security vulnerability in all versions of asp.net - http://www.microsoft.com/technet/security/advisory/2416728.mspx . This vulnerability could potentially allow an attacker to view the web.config file or view data from an encrypted ViewState. To workaround the problem, the customErrors element in the web.config file needs to be configured to always result in the same error page regardless of the error caused on the server.  The mode attribute must be set to “On” and the defaulktRedirect attribute must be set to a default error page. The issues is further described at http://weblogs.asp.net/scottgu/archive/2010/09/18/important-asp-net-security-vulnerability.aspx .

September 19, 2010

Getting types of .NET built-in types

The GetType() method can be used to get the actual type of any value or reference type. In the following example I have demonstrated how to get the actual type of some value types.
Code for aspx page:
   1:  int: <asp:Literal ID="Lint" runat="server"></asp:Literal><br />
   2:  double: <asp:Literal ID="Ldouble" runat="server"></asp:Literal><br />
   3:  boolean: <asp:Literal ID="Lboolean" runat="server"></asp:Literal><br />
   4:  string: <asp:Literal ID="Lstring" runat="server"></asp:Literal><br />
   5:  uint: <asp:Literal ID="Luint" runat="server"></asp:Literal><br />





Code for code behind page:

   1:  protected void Page_Load(object sender, EventArgs e)
   2:  {
   3:              int i = 0;
   4:              Lint.Text = i.GetType().ToString();
   5:   
   6:              double d = 1.0;
   7:              Ldouble.Text = d.GetType().ToString();
   8:   
   9:              bool b = false;
  10:              Lboolean.Text = b.GetType().ToString();
  11:   
  12:              uint ui = 1;
  13:              Luint.Text = ui.GetType().ToString();
  14:   
  15:              string s = "";
  16:              Lstring.Text = s.GetType().ToString();
  17:  }



When the page is run, the page displays the actual types for this variables used on the page.
Reference: Shahed Kazi at AspNetify.com