Saturday, September 19, 2009

Some important .NET functions. Part – 1

.NET is a big ocean. Each day when I work I find one or the other function which are very useful in day to day programming. So I thought I will start a series of blog on some .NET function which I feel  are very useful. Here is the first list of function.

Recently while working in a ASP.NET project I wanted to make use of colors given by the designers in C# code behind file and wanted to change the color of ASP.NET controls. You will be thinking what is so hard in this. There is nothing much other than the color was given in hexadecimal value, like #657468, #AA3212 etc, and .NET controls only understand System.Drawing.Color structure values. So there is my problem. I need to convert the hexadecimal values to System.Drawing.Color values. Luckily I didn’t had to break my head for solving this problem, .NET provides a class just to solve my problem. The class is called ColorTranslator. The method which helped me is FromHtml. Code is pasted below.

DropDownList1.BackColor = System.Drawing.ColorTranslator.FromHtml("#345678");
DropDownList1.BorderColor = System.Drawing.ColorTranslator.FromHtml("Yellow");

The FromHtml method can take the name of the color as well as the hexadecimal value/HTML value. Some other methods of the class are listed below.

ColorTranslator.ToHtml(Color theColor): This is just the opposite of FromHtml i.e. it converts the color to HTML values. So when one wants to convert color to HTML color values/Hexadecimal color value one can make use of the ToHtml method. Sample code is pasted below.

DropDownList1.BackColor =
System.Drawing.ColorTranslator.FromHtml("#ff5678");
DropDownList1.BorderColor =
System.Drawing.ColorTranslator.FromHtml("Yellow");
Console.WriteLIne(System.Drawing.ColorTranslator.ToHtml
(DropDownList1.BackColor));//Output - #ff5678
Console.WriteLine(System.Drawing.ColorTranslator.ToHtml
(DropDownList1.BorderColor));//Output - Yellow

ColorTranslator.ToWin32(Color c) and ColorTranslator.FromWin32(int w32): ToWin32 converts the Color object to the equivalent windows color value whereas FromWin32 does just the opposite i.e. converts a Win32 color value to a System.Drawing.Color object. Sample codes are pasted below.

Console.WriteLine(System.Drawing.ColorTranslator.ToWin32
(System.Drawing.Color.Red));//Output – 255.
System.Drawing.Color color =
System.Drawing.ColorTranslator.FromWin32(255);

ColorTranslator.ToOLE(Color col) and ColorTranslator.FromOLE(int oleValue): ToOLE method converts Color object to OLE equivalent color value and FromOLE converts OLE color value to System.Drawing.Color object.

My ever useful System.IO.Path class

Over my career the class which is mostly used by me is the System.IO.Path class. Though the class has got only 15 or so static methods I use this class a lot while working with file paths. Let me first start with my favorite Combine method of the Path class which I use the most.

Path.Combine(string path1, string path2): The combine method combines two paths and returns you the combined path. The method takes care to add the necessary directory separator before combining the two paths. Also the combine method can be used to combine paths with wild characters like *.pdf or sa*.txt etc. If wild characters are used the combine method takes them as search criteria and doesn’t throw any error. E.g pasted below.

System.IO.Path.Combine(Server.MapPath("."), "images")
//Output: "C:\\aaa\\bb\\WebSite1\\images"
System.IO.Path.Combine(System.IO.Path.Combine(Server.MapPath
("."), "images"), "*.gif")
//Output: "C:\\aaa\\bb\\WebSite1\\images\\*.gif"

Path.GetExtension(string thePath): The GetExtension method returns the extension along with “.” from the path argument passed. So if you want to retrieve only extension from a file path just pass the file path to the GetExtension method and there you go you will have your extension. GetExtension method returns an empty string if the path doesn’t have any extension, also it returns null if the path argument is null. E.g. pasted below.

string filePath = System.IO.Path.Combine(System.IO.Path.Combine(Server.MapPath
("."),  "images"), "*.gif");
Console.WriteLine(System.IO.Path.GetExtension(filePath));
//Output: .gif

Path.GetFileName(string thePath): This method returns the filename with extension from the specified path argument. Returns an empty string if there are no file name. Sample code is pasted below.

string filePath = System.IO.Path.Combine(System.IO.Path.Combine(Server.MapPath
("."), "images"), "tick.gif");
Console.WriteLine(System.IO.Path.GetFileName
(filePath));//Output: tick.gif

Path.ChangeExtension(string filePath, string newExt): This method changes the extension of the file. The method takes two arguments, filename with extension along with/without path  and second one is the new extension with “.” which needs to be applied. If period is not there in the new extension argument then the method adds one. If you just wants to remove the extension then just pass a null value as the new extension parameter. Sample code is given below.

string filePath = System.IO.Path.Combine(System.IO.Path.Combine(Server.MapPath
("."), "images"), "tick.gif");
Console.WriteLine(System.IO.Path.ChangeExtension
(filePath, "jpg"));//Output: C:\\aaa\\bb\\WebSite1
\\images\\tick.jpg

Path.GetFileNameWithoutExtension(string filePath): Function returns the file name without extension. E.g. is shown below.

string filePath = System.IO.Path.Combine(System.IO.Path.Combine(Server.MapPath
("."), "images"), "tick.gif");
Console.WriteLine(System.IO.Path.ChangeExtension
(filePath, "jpg"));//Output: tick

Path.GetRandomFileName(): Returns a random string. The string is generated using cryptographic principles. The string can be used as folder or file name. Sample code is pasted below.

Console.WriteLine(System.IO.Path.GetRandomFileName())//Output: x4zxg224.hva

Path.GetTempFileName(): Function returns a string with a file name along with temporary folder path. The filename returned has a .tmp extension. The method creates a file with .tmp in the temporary folder and then returns the filename along with the temprorary folder path where the file is created. Sample code with output is pasted below.

Console.WriteLine(System.IO.Path.GetTempFileName())//Output: C:\\Documents and Settings\\sandeep\\Local Settings\\Temp\\tmp20C.tmp

Path.GetTempPath(): GetTempPath function returns the system’ temporary folder path. Sample code is pasted below.

Console.WriteLine(System.IO.Path.GetTempPath());//Output: C:\\Documents and Settings\\sandeep\\Local Settings\\Temp\\

Path.HasExtension(string filePath): This function checks whether the file path passed has a file extension. If extension is found the function returns true else false.

So these are some of my favorite and useful functions. Will publish some more over the course of time till then try to know more.

Sandeep

No comments:

Post a Comment

Please provide your valuable comments.