Project Description
One or more watched folders are defined. Each folder is given a category. A Windows service watches the folders. When a file is dropped into a folder, the system inserts it into a SQL Server table. Admin is done via a Windows forms app written using Developer Express controls. C#

Overview

Administration - Windows Forms Application

The Windows Forms application is the general administrative tool for this system. Use it to create and manage categories and watched folders. Also use this tool to search and view the application log, which is written to a SQL Server table. Note that all logged information is also written to the system event log. This Windows Forms application is also used to search for and to retrieve files which have been saved to the archive.

Category Search Results

Category Search Results

Watched Folder Main Form

Note that because Archived File is a child entity of Watched Folder, the archived file are available in a tab below the watched folder detail.
Watched Folder Main Form

Windows Service

The Windows service does the work of watching the folder and processing files. Each file that is dropped into a watched folder will be picked up by the service, inserted into the database and then removed from the file system. Each archived file record will be associated with both the folder where it was originally dropped and also the category that was assigned to that folder at the time that the file was processed.

Core Code of this Windows Service

        protected void pollForFiles()
        {
            logEntry("Starting poll for files", EventLogEntryType.Information);

            // Get a collection of all watched folders
            WatchedFolderList watchedFolderList = (WatchedFolderList)sharedRepository.FindWatchedFolder(-1, null);

            foreach (WatchedFolder watchedFolder in watchedFolderList)
            {                                
                string[] arrayOfFiles = new string[0];
                try
                {
                    arrayOfFiles = System.IO.Directory.GetFiles(watchedFolder.UncOrLocalFolder, "*.*", SearchOption.AllDirectories);
                }
                catch (Exception ex)
                {
                    logEntry("Error reading from folder " + watchedFolder.UncOrLocalFolder + " - " + ex.Message, EventLogEntryType.Error);
                }

                int countOfFiles = arrayOfFiles.GetUpperBound(0) + 1;
                if(countOfFiles > 0)
                    logEntry("Note: Count of files = " + countOfFiles.ToString() + " in and under folder " + watchedFolder.UncOrLocalFolder, EventLogEntryType.Information);

                foreach (string eachFileName in arrayOfFiles)
                {
                    string thisEachFileName = eachFileName.ToUpper();

                    string fileNameOnly = "";
                    for (int i = thisEachFileName.Length-1; i >= 0; i--)
                    {
                        if (thisEachFileName[i].ToString() == "\\")
                        {
                            break;
                        }
                        else
                        {
                            fileNameOnly = thisEachFileName[i].ToString() + fileNameOnly;
                        }
                    }

                    try
                    {
                        // Get the category
                        ArchiveCategory theCategory = watchedFolder.ParentRelatedArchiveCategoryEntityObject;

                        // Create a new Archived File record
                        ArchivedFile newArchivedFile = theCategory.ChildRelatedArchivedFileList.AddNew();
                        newArchivedFile.Description = fileNameOnly;
                        FileAttachment fileAttachment = new FileAttachment(fileNameOnly,  readFileToByteArray(eachFileName));
                        newArchivedFile.FileAttachment = fileAttachment;

                        // Associate this new Archived File with the Watched Folder where it was dropped
                        watchedFolder.ChildRelatedArchivedFileList.Add(newArchivedFile);

                        // Save via Repository
                        sharedRepository.SaveWatchedFolder(watchedFolder);
                        sharedRepository.SaveArchiveCategory(theCategory);
                        sharedRepository.SaveArchivedFile(newArchivedFile);

                        logEntry("Sucessfully archived file " + eachFileName, EventLogEntryType.Information);

                        // Remove the file from the watched folder
                        File.Delete(eachFileName);
                    }
                    catch (Exception ex)
                    {
                        logEntry("Failed to process file " + eachFileName + " - " + ex.Message, EventLogEntryType.Error);
                    }
                }
            }
        }

        public byte[] readFileToByteArray(string fileToRead) 
        { 
            FileStream fs = new FileStream(fileToRead, FileMode.Open, FileAccess.Read); 
            BinaryReader br = new BinaryReader(fs); 
            Byte[] allBytes = br.ReadBytes((Int32)fs.Length); 
            br.Close(); 
            fs.Close(); 
            return allBytes; 
        } 

Last edited Nov 26, 2009 at 3:25 PM by Venatek, version 10