Tuesday, September 15, 2009

File Monitoring in .NET

File monitoring applications can be easily developed in .NET using the FileSystemWatcher class. This class listens to changes in files system and raises events whenever changes happen. This article explains how to monitor changes in files and subdirectories of a directory using the FileSystemWatcher class.
The FileSystemWatcher is available within the System.IO namespace. We need to include the System.IO namespace for accessing the FileSystemWatcher class. In order to specify the folder to monitor we have to set the Path property of the FileSystemWatcher as shown below:

FileSystemWatcher fsw = new FileSystemWatcher();
fsw.Path = "c:\\fileWatcher";
If you want to monitor changes to a specific file then the Filter property should be used along with the Path property. The Filter property should be set to file name and the Path property should be set to the path of the folder as shown below
fsw.Path = "c:\\fileWatcher ";
fsw.Filter = "TestWatchFile.txt";
Wild card characters can also be used to specify set of files as shown below
fsw.Filter = "*.txt";
The FileSystemWatcher to begin monitoring we have to set the EnableRaisingEvents property to true
fsw.EnableRaisingEvents = true;

As mentioned before the FileSystemWatcher raises events whenever changes like creation, deletion etc happens to the specific folder or file. The events that are raised by FileSystemWatcher are Changed, Created, Deleted, Error and Renamed.

We need to write appropriate event handlers to handle these events. The FileSystemEventHandler has an argument named FileSystemEventArgs, which provides useful event data. The data provided by the FileSystemEventArgs are ChangeType which gives the type of change that occurred and Path which gives the full path of the affected file or folder.

A sample Changed event handler is given below
fsw.Changed += new FileSystemEventHandler(fsw_changed);
fsw.Deleted += new FileSystemEventHandler(fsw_changed);
fsw.Created += new FileSystemEventHandler(fsw_changed);
private void fsw_changed(object Sender,FileSystemEventArgs e)
{
MessageBox.Show("The file" + e.FullPath+ " was " + e.ChangeType.ToString() + " on " + System.DateTime.Now.ToString());
}

FileSystemObject class can be used to monitor files or folders by setting appropriate properties and handling the events raised by the class.

Monday, September 14, 2009

Executing ASP Page in ASP.NET

When migrating applications from ASP to ASP.NET there may be situations where you would like to use some of the existing ASP code within ASP.NET pages. This how can we achieve to do so.

We can embed ASP pages within ASP.Net pages by suing screen-scraping mechanisms provided by the WebRequest and WebResponse classes, which are within the System.Net namespace. These two classes provide a request/response model for accessing data from Internet. Using the WebRequest class we can create a request to the asp page, which you need to embed within ASP.NET page. The WebResponse class is used to receive the response generated by the request. We then use the StreamReader class to get the contents of the response and display it on the screen.
All this functionality is encapsulated within a static method of a class as given in the code below. This static function accepts the name of the ASP page to be embedded. It gets response from the ASP page and returns the response content as a string.
public class ASPWithInASPNet
{
public static void GetASPResults(string AspPage)
{
try
{
//CHECK IF THE GIVEN PAGE NAME IS A URL
Regex objRegex = new Regex(@"^http\://[a-zA-Z0-9\-\.]+[a-zA-Z]{2,3}(/\S*)?$");
string StrPath;
//IF THE PAGE NAME IS URL
if(objRegex.IsMatch(AspPage))
{
StrPath = AspPage;
}
//IF THE PAGE NAME IS NOT A URL
else
{
Uri RequestURI = HttpContext.Current.Request.Url;
StrPath = RequestURI.Scheme+ "://" + RequestURI.Host +
HttpContext.Current.Request.ApplicationPath + "/";
StrPath = StrPath + AspPage;
}
//CREATE A REQUEST TO THE ASP PAGE
HttpWebRequest WebReq =
(HttpWebRequest)WebRequest.Create(StrPath);
// GET THE RESPONSE
WebResponse WebRes = WebReq.GetResponse();
StreamReader SR = new StreamReader(WebRes.GetResponseStream());
HttpContext.Current.Response.Write(SR.ReadToEnd());
}
catch(WebException Ex)
{
HttpContext.Current.Response.Write( Ex.Message);
}
}
}
This class can be used in any of you ASP.Net pages as shown in the sample below.
private void Page_Load(object sender, System.EventArgs e)
{
   ASPWithInASPNet.GetASPResults("http://localhost/aspconvertProj/default.asp");
}
Note: This functionality can be used to embed not only ASP pages but also any web page like JSP, HTML etc.

Friday, September 04, 2009

Build the Newest Silverlight 3 Applications with Expression

Silverlight 3 and Expression Studio 3 help you create and deploy Web sites and rich Internet applications more easily than ever. Download Silverlight 3 and the Expression Studio 3 60-day trial today.

View article...

Wednesday, September 02, 2009

HTML 5

HTML 5 is the next major revision of HTML (Hypertext Markup Language), the core markup language of the World Wide Web.

HTML 5 is the proposed next standard for both HTML 4.01 and XHTML 1.0, as development on the next version of the latter has stopped. HTML 5 was initially said to become a game-changer in Web application development, making obsolete such plug-in-based rich Internet application (RIA) technologies as Adobe Flash, Microsoft Silverlight, and Sun JavaFX. Such applications would be made obsolete by specifying a standard video codec for all browsers to use. However, in December 2007, the editor of the burgeoning draft specification dropped the recommendation of the free software Theora and Vorbis codecs, after opposition from Apple and Nokia. This means HTML 5 does not currently specify a common video codec for Web development.

The ideas behind HTML 5, originally referred to as Web Applications 1.0, were pioneered in 2004 by the Web Hypertext Application Technology Working Group (WHATWG); HTML 5 incorporates Web Forms 2.0, another WHATWG specification. The HTML 5 specification was adopted as the starting point of the work of the new HTML working group of the W3C in 2007.

Difference between HTML4 and HTML5

Click Here for more.

Friday, August 28, 2009

How to Change Font-Size, Font-Family,Font-Style at RunTime

Label lblCarrier = new Label();

lblCarrier.Text = "Carrier Name:"
lblCarrier.AutoSize = true;
Font font = new Font("Arial", 9, FontStyle.Bold);
lblCarrier.Font = font;

Hope this helps :-)