Click here to Skip to main content
12,948,566 members (60,727 online)
Click here to Skip to main content
Add your own
alternative version


43 bookmarked
Posted 15 Feb 2006

File Watcher that Auto Moves Your Working Files from the VS.NET Project Folder

, 23 Feb 2006
Rate this:
Please Sign up or sign in to vote.
An article on separating your working file from Visual Studio .NET 2003 solutions.

Sample Image - WenjunSFW.jpg


Visual Studio .NET 2003 and its earlier versions are great, and are becoming the first choices for .NET application developers. You create a new project, and then develop and compile the code. But, there is a pain when you want to deploy your working file to production. Although you can create a deployment project to build the MSI installation, in many cases, you don't want to build the MSI. Instead of a bunch of junk files mixed with your working files in your project folder, you simply want to have a clean folder containing only the working folders/files and hand it over to your clients.

When you create a project with Visual Studio .NET 2003, it will generate several files automatically:


If your project is linking with SourceSafe, you will have more junk files. These files normally need not be included in your application when you ship your application to clients.

Suppose you have a very big project that consists of several projects; to separate the working files from these projects/solutions is a big pain.

This article tries to demonstrate how to solve this issue. I have built a file watcher object to monitor the project root folder; whenever a file/folder created, deleted, renamed, or hanged, it will move this file/folder to a separate destination (project release folder).

In this case, the working files/folders will be moved to the destination while your are developing. At the time your development is done, your clean working folder has been created. You just copy the whole destination folder and deploy it to your client. Sounds good?


The general ideas to solve this issue are:

  1. Define two folders:
    • FROM: the root folder of your developing project.
    • TO: the deployment folder to which you want to deploy your application.

      This folder contains two sub folders: Engine and Bin. Engine is used for content files and related folders. Bin is used for all .dll and .pdb files.

  2. Create a configuration file indicating where and how to process your project. The configuration file is in XML format as follows:
  3. Build a file watcher to monitor the FROM folder; based on the configuration file, it moves/changes the files/folders from the source folder to the destination folder.

Using the code

There is only one class to perform the file monitoring job: FileWatcher. There are three important methods in this class: ProcessWatcher, ProcessTask, and CopyDeleteFile. I will describe these methods one by one.

ProcessWatcher creates the instance of the watcher object. It creates four events: FileChanged, FileCreated, FileReNamed, and FileDeleted. In the configuration file, you specify which event you want to track.

/// <summary>
/// Create the instance of the watcher object
/// and fire the monitoring events.
/// After this method has executed,
/// the process is ready to monitor
/// </summary>
private void ProcessWatcher()
    sourcePath = GetWatcherInfo("Source");
    pDestinationPath = GetWatcherInfo("ProjectDestination");
    eDestinationPath = GetWatcherInfo("EngineDestination");
    watchFileExtension = GetWatcherInfo("FileExtensions");
    trace = GetWatcherInfo("Trace").ToLower();
    string eventName = GetWatcherInfo("Events");
    string [] arrEvents = eventName.Split(",".ToCharArray());
    int i = 0;

    pDest = pDestinationPath.Split(";".ToCharArray());

    WatchFile = new FileSystemWatcher(sourcePath, "*");
    WatchFile.IncludeSubdirectories = true;
    WatchFile.NotifyFilter = NotifyFilters.Size | 
              NotifyFilters.LastWrite | 
              NotifyFilters.FileName | 

    for (i=0;i<arrEvents.Length;i++)
            case "Changed":
                WatchFile.Changed += new 
            case "Created":
                WatchFile.Created += new 
            case "Renamed":
                WatchFile.Renamed += new 
            case "Deleted":
                WatchFile.Deleted += new 

    WatchFile.EnableRaisingEvents = true;

Whenever the watcher fires an event, it then goes here to check what kind of process to execute.

/// <summary>
/// Decide what kind of process needs to perform
/// </summary>
/// <param name="e">FileSystemEventArgs event argument</param>
/// <param name="checkType">Fired event type</param>
/// <param name="fileOrFolder">Is this a file or folder?</param>
private void ProcessTask(FileSystemEventArgs e, 
                         string checkType, string fileOrFolder)
    string fileName = string.Empty;
    fileName = GetNameOnly(e.Name);

        case "Created":
            CheckFolder(e.Name, fileName, fileOrFolder);
            CopyDeleteFile(e.FullPath, e.Name, fileName, 
                           "", fileOrFolder, false);
        case "Changed":
            CopyDeleteFile(e.FullPath, e.Name, fileName, 
                           "", fileOrFolder, false);
        case "Deleted":
            if (fileOrFolder == "file")
                for (int i=0;i<pDest.Length;i++)
                    File.Delete(pDest[i] + "\\" + fileName);
                    File.Delete(pDest[i] + "\\" + e.Name);
                Directory.Delete(eDestinationPath + 
                                 "\\" + e.Name, true);

    if (trace == "yes")
        WriteLog(e, checkType);

Finally, the execution method CopyDeleteFile: if it's a rename request, this method deletes the mapping file in the destination folder first, then copies the renamed file to the destination mapped folder. If the file type is a folder, it does the same behavior as for a file.

/// <summary>
/// Whenever a file/folder has been renamed, deleted, created or modified, 
/// the destination will do the same process accordingly.
/// </summary>
/// <param name="source">The folder path is been monitoring</param>
/// <param name="watchedNamePath">The changed file/folder path</param>
/// <param name="fileName">Destination file path to be mapped</param>
/// <param name="oldName">Destination file/folder
///       path before the name changing.</param>
/// <param name="fileOrFolder">Indicates the
///           monitored object is a file or folder</param>
/// <param name="isRename">Is this fried for rename?</param>
private void CopyDeleteFile(string source, string watchedNamePath, 
        string fileName, string oldName, 
        string fileOrFolder, bool isRename)
    //Copy to project directory
    if(fileName.IndexOf(".dll") > 0 || fileName.IndexOf(".pdb") > 0)
        for (int i=0;i<pDest.Length;i++)
            File.Delete(pDest[i] + "\\" + fileName);
            File.Copy(source, pDest[i] + "\\" + fileName, true);
            File.SetAttributes(pDest[i] + "\\" + fileName, 
        //Copy to engine directory. Only copy when file changed
        if (oldName == "" && fileOrFolder == "file")
            File.Copy(source, eDestinationPath + "\\" + 
                              watchedNamePath, true);
            File.SetAttributes(eDestinationPath + "\\" + 
                               watchedNamePath, FileAttributes.Normal);
            if (fileOrFolder == "file")
                File.Delete(eDestinationPath + "\\" + oldName);

                if (isRename)
                    File.Copy(source, eDestinationPath + 
                          "\\" + watchedNamePath, true);
            else if (fileOrFolder == "folder" && oldName != "")
                Directory.Move(eDestinationPath + "\\" + oldName, 
                      eDestinationPath + "\\" + watchedNamePath);

To Run the Demo Program

Download the demo Zip file and unzip it to your c: drive. Open the root FileWatchDemo folder, you should see the following files and folders:


Double click the FileWatcher.exe, and copy a DLL to the C:\FileWatcherDemo\TestFolder\From folder. You should see that this DLL has been added into the C:\FileWatcherDemo\TestFolder\To\bin folder.

Copy a GIF file to the C:\FileWatcherDemo\TestFolder\From folder, you should see this GIF file has been added into the C:\FileWatcherDemo\TestFolder\To\engine folder.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
United States United States
Wenjun Wang is a senior software engineer from San Diego, USA. Wenjun is Microsoft Certified Solution Developer (MCSD). He has many years of Windows and web application developing experiences.

Currently, he is a tech lead in a software development company. He is running his personal website for sharing IT technologies. He loves his wife and daughters. His baby is cute.


You may also be interested in...


Comments and Discussions

QuestionMultiple folders? Pin
Clabbe_W16-Mar-07 11:02
memberClabbe_W16-Mar-07 11:02 
GeneralGood article Pin
vektuz23-Feb-06 10:13
membervektuz23-Feb-06 10:13 
GeneralGood idea, but... Pin
leddt16-Feb-06 2:17
memberleddt16-Feb-06 2:17 
GeneralRe: Good idea, but... Pin
Ben Meyers16-Feb-06 5:18
memberBen Meyers16-Feb-06 5:18 
GeneralRe: Good idea, but... Pin
wenjunw.comnt816-Feb-06 12:44
memberwenjunw.comnt816-Feb-06 12:44 
GeneralRe: Good idea, but... Pin
James H22-Feb-06 5:59
memberJames H22-Feb-06 5:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170524.1 | Last Updated 23 Feb 2006
Article Copyright 2006 by wenjunw.comnt8
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid