Code Simplified – Viral Sarvaiya

Code Simplified – Viral Sarvaiya, Web Developer Friends, dot net Developer, Sql Server Developer

Archive for September, 2011

Monitoring the File System

Posted by Viral Sarvaiya on September 20, 2011

Today i get new thing in my project that use FileSystemWatcher Class, so here i explain how to monitoring the file system,

You can use the FileSystemWatcher class (part of the System.IO namespace) to respond to updated files, new files, renamed files, and other updates to the file system. First, create an instance of FileSystemWatcher by providing the path to be monitored. Then, configure roperties of the FileSystemWatcher instance to control whether to monitor subdirectories and which types of changes to monitor. Next, add a method as an event handler. Finally, set the FileSystemWatcher.EnableRaisingEvent property to true. The following code sample demonstrates a basic usage.

// Create an instance of FileSystemWatcher
FileSystemWatcher fsw = new FileSystemWatcher(Environment.GetEnvironmentVariable("USERPROFILE"));
// Set the FileSystemWatcher properties
fsw.IncludeSubdirectories = true;
fsw.NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastWrite;
// Add the Changed event handler
fsw.Changed += new FileSystemEventHandler(fsw_Changed);
// Start monitoring events
fsw.EnableRaisingEvents = true;

Handling FileSystemWatcher Events When a file is changed that meets the criteria you specify, the CLR calls the FileSystemWatcher.Changed event handler for all changes, creations, and deletions. For files and folders that are renamed, the CLR calls the FileSystemWatcher.Renamed event handler. The previous code sample added the fsw_Changed method to handle the Changed event. The following code sample shows a simple way to handle the event:

static void fsw_Changed(object sender, FileSystemEventArgs e)
// Write the path of a changed file to the console
Console.WriteLine(e.ChangeType + ": " + e.FullPath);

You can use a single event handler for the Changed, Created, and Deleted events. The FileSystemEventArgs parameter provides the path to the updated file and the type of change that occurred. If you need to respond to files that are renamed, you need to create an event handler that accepts a RenamedEventArgs parameter instead of a FileSystemEventArgs parameter, as the following code sample demonstrates:

static void fsw_Renamed(object sender, RenamedEventArgs e)
// Write the path of a changed file to the console
Console.WriteLine(e.ChangeType + " from " + e.OldFullPath + " to " + e.Name);

Configuring : FileSystemWatcher Properties You can configure the following properties of the FileSystemWatcher class to control which types of updates cause the CLR to throw the Changed event:

Filter : Used to configure the filenames that trigger events. To watch for changes in all files, set the Filter property to an empty string (“”) or use wildcards (“*.*”). To watch a specific file, set the Filter property to the filename. For example, to watch for changes in the file MyDoc.txt, set the Filter property to “MyDoc.txt”. You can also watch for changes in a certain type of file. For example, to watch for changes in text files, set the Filter property to “*.txt”.

NotifyFilter Configure the types of changes for which to throw events by setting NotifyFilter to one or more of these values:

  • FileName
  • DirectoryName
  • Attributes
  • Size
  • LastWrite
  • LastAccess
  • CreationTime
  • Security

Path : Used to define the folder to be monitored. You can define the path using the FileSystemWatcher constructor. You can watch for the renaming, deletion, or creation of files or directories. For example, to watch for the renaming of text files, set the Filter property to “*.txt” and call the WaitForChanged method with a Renamed value specified for its parameter.


Posted in .Net, ASP.NET, feature | Tagged: , , , , , , , | Leave a Comment »

NHibernate for .NET

Posted by Viral Sarvaiya on September 5, 2011

Recently I have do some work in new technology – NHibernate.

NHibernate is same as JAVA’s Hibernate Core in .net framework. NHibernate is object-relational mappers(ORM). It handles persisting plain .NET objects to and from an underlying relational database. with an XML description of your entities and relationships, NHibernate automatically generates SQL for the objects.Persistent classes do not need to implement any interface or inherit from a special base class. This makes it possible to design the business logic using plain .NET (CLR) objects and object-oriented idiom.

NHibernate key features:

Natural programming model – NHibernate supports natural OO idiom; inheritance, polymorphism, composition and the .NET collections framework, including generic collections.

Native .NET – NHibernate API uses .NET conventions and idioms

Support for fine-grained object models – a rich variety of mappings for collections and dependent objects

No build-time bytecode enhancement – there’s no extra code generation or bytecode processing steps in your build procedure

The query options – NHibernate addresses both sides of the problem; not only how to get objects into the database, but also how to get them out again

Custom SQL – specify the exact SQL that NHibernate should use to persist your objects. Stored procedures are supported on Microsoft SQL Server.

Support for “conversations” – NHibernate supports long-lived persistence contexts, detach/reattach of objects, and takes care of optimistic locking automatically

Free/open source – NHibernate is licensed under the LGPL (Lesser GNU Public License)


For More details :

Posted in .Net, feature | Tagged: , , , , | Leave a Comment »

%d bloggers like this: