.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal

Home >> Forum >> C# >> Post New QuestionBookmark and Share Subscribe to Forum

File rename using FilesystemWatcher in C#.net

Posted By: Narayanan     Posted Date: March 06, 2012    Points:5   Category :C#
In my Project, I am using Datagridview and Context Menu .I fill a information from a Particular folder .it contains file name, file Type and Size.User wants to rename a file when user select a row in the datagridview and right click on selected row on the datagridview and context Menu view..user will change the File Name in the datagridview and automatically change in the Particular folder.How to do this? Is it possible to use filewatchersystem for this task?I If "yes" Please send a code or link.

Author: Mahesh             
Posted Date: March 07, 2012     Points: 20   

Follow the following code, you need to modify the code as per requirement,

// Create a new FileSystemWatcher and set its properties.
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = ""; //provide the path for the folder
/* Watch for changes in LastAccess and LastWrite times, and
the renaming of files or directories. */
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;
// Only watch text files.
watcher.Filter = "*.txt";

// Add event handlers.
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnChanged);
watcher.Deleted += new FileSystemEventHandler(OnChanged);
watcher.Renamed += new RenamedEventHandler(OnRenamed);

// Begin watching.
watcher.EnableRaisingEvents = true;

// Wait for the user to quit the program.
Console.WriteLine("Press \'q\' to quit the sample.");
while (Console.Read() != 'q') ;

// Define the event handlers.
private static void OnChanged(object source, FileSystemEventArgs e)
// Specify what is done when a file is changed, created, or deleted.
Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);

private static void OnRenamed(object source, RenamedEventArgs e)
// Specify what is done when a file is renamed.
Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath);

hope it may help you...


Author: Narayanan             
Posted Date: March 12, 2012     Points: 20   

Hi Mahesh,

Thanks for your reply and Please read 2rd line in my question. I am using Datagridview.So Please tell me How to achieve this?

Regards, Lakshmi Naraayanan.S

Post Reply

You must Sign In To post reply
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend