Posts

Showing posts with the label DOT NET

Download File using Dot Net

In this post, I will demonstrate how easy it is to download a file from a web server to your local hard drive. In most scenarios, you may browse the net using your web browser and click on a hyperlink to start a downloading of a file from a remote site. Some of you may be interested in how to download this using their application code using Dot Net. This can be very easily achieved using the WebClient class under System.Net namespace. First, import the System.Net namespace. using System.IO; The above LOC will do so. Now, create an instance of WebCleient class. WebClient client = new WebClient(); Now, call the DownloadFile method client.DownloadFile(<url>, @"<fileDestinationName>.<filetype>"); Have fun. Cheers !!!!

ASP Response.Write newline

Hi, all of you who have used ASP may have encountered situations where you require to write a text output to the screen. It can be easily done by using Response.Write("Hello world"); However, if you wish to append some more text to a newline, it would not work in the following ways. Response.Write("Hello world \n"); Response.Write("Good day."); The output of the above lines would be Hello world Good day. and NOT Hello world Good day. In order to overcome this, you can very simple place an html break tag inside the code. Response.Write("Hello world"); Response.Write("<br/>"); Response.Write("Good day."); The output would be the following. Hello world Good day. Cheers !!!

ASP Regular expression to check length of textbox value

Hi, here is a regular expression validator in ASP which you can use to check if a certain textbox has less than a certain number of characters. <asp:regularexpressionvalidator runat="server" id="lengthRegex" controltovalidate="">" validationexpression=".{1,<n>}" errormessage="Please note that there should be less than <n> number of characters in the textbox"> </asp:regularexpressionvalidator> So, if you intend to make the textbox accept a number of characters in a range(say between 5 to 10) then the following changes would work <asp:regularexpressionvalidator runat="server" id="lengthRegex" controltovalidate="">" validationexpression=".{5,10}" errormessage="Please note that there should be between 5 to 10 characters in the textbox"</asp:regularexpressionvalidator> Suppose you need to limit the characters to only alphanumerics, then use the...

Dot NET Regular Expression

Hi, in this post, I will demonstrate how to use a Regular Expression using the Dot NET Framework. If you are new to Regular Expressions, this post would not be of much use. This is intended for an audience who knows regular expressions and need to implement a regular expression in their Dot NET code. There may be scenarios where you need to match a certain string with a Regular Expression. Using the Dot NET Framework, this becomes relatively easy. First, you need to import the namespace by defining using System.Text.RegularExpressions; Then, you can create a Regex object by calling its constructor. This is an overloaded constructor. I will show only one use of it. (You can explore the other!!!) Regex regularExpression = new Regex(" "); This would create a Regex object. Once created, you can call several methods to get the job done. I will show how the Replace method can be used to identify a character pattern and replace it with a character set of your own. (A good example i...

Dot Net Date Formatting

Hi, all of you people may have encountered situation where you wish to format your date for a specific format. This becomes very simple with Dot NET Framework. Suppose you have a date to be displayed. Let the date be 1st January 2009 Now, this can be displayed in various formats. The respective date-format-string is displayed alongside Eg: Displaying these can be done using the following LOC. DateTime today=DateTime.Now; string formattedDate=today.ToString( ); Eg: string formattedDate=today.ToString("dd-MM-yyyy"); =>01-Jan-2009 string formattedDate=today.ToString("dd/MM/yyyy"); =>01-Jan/2009 string formattedDate=today.ToString("d-MMM-yy"); =>1-January-2009 Try out other combinations of your own and experience the power!!!!!!! Format Respective date-format-string 01/01/09 'dd/MM/yy' 01 Jan 09 'dd MM yy' 01 Jan 2009 'dd MM yyyy' 1 January 2009 'd MMM yyyy'

Dot Net Writing to a File

A very simple LOC which may become very useful. File.WriteAllText("C:\\a.txt","Hello World");

How to Browse Folders using Dot NET

Image
Most of you may have come up with scenarios where you wish to let the user browse for a specific folder or file. This is most commonly occured when you are requested to open a specific file in Word or Power Point. I would be using Visual Studio 2005 and DOT NET Framework 2.0 DOT NET 2.0 Framework supports several useful dialogs. In this, we will use the "FolderBrowseDialog" class for the example. It can be very easily found in the "ToolBox" under "Dialogs" category. Simply drag and drop one on to your Form. Add a Button also for the demo. Then, double click on the Search Button and it would take you to the Click Event of the Button. Inside that, place the following LOC. DialogResult result; result = folderBrowserDialog1.ShowDialog(); if (result == DialogResult.OK) { MessageBox.Show("Selected Path :" +folderBrowserDialog1.SelectedPath); javascript:void(0) } Lets anal...