Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
What I want is to scan a specific folder "C:\SomeFolder\" and look for .txt files and look for updates each n seconds. Then put these file names in a SQL table (name, lines...) I know that this gets the files
string[] filePaths = Directory.GetFiles(@"c:\SomeFolder\", "*.txt"); 
but how to refresh the research and to store them in a file or the SQL table ? I tried to use the Append but I had problems each time especially that it needs to run as a windows service.
Posted
Comments
Vinay Mistry 18-Aug-14 2:23am    
Before append file check single entry already exist or not. if not exit then append that entry otherwise check another.

1 solution

Hm, well i would base my implementation on the FileSystemWatcher which in fact doesn't scan but reports changes, it does fire quite a lot of events so it needs a bit of pampering. Then use a timer to flush changes to your database to avoid being too chatty, as the FSW triggers changes on a per-file style.

Didn't really get your storage problems. If you clear the file or database between changes you'll only catch the latest, if you're simply logging changes you would have a timestamp on your row regardless where it is.
If you're running a windows service, it's running in a context and that context must have rights to the filesystem on the location of your file. If you can use a database you won't have to worry about service user context at all.

Here's my 5 cents of a somewhat defendable 'scanner' :)

static void Main(string[] args)
        {
            using (var fs = new FolderSpy("c:\\temp\\play"))
            {
                Console.WriteLine("Press any key to exit");
                Console.ReadKey();
            }
        }

        public class FolderSpy : IDisposable
        {
            private FileSystemWatcher _fileSystemWatcher;
            private static readonly object Lockable = "Lockable";
            private Timer _timer = new Timer(TimeTicks);
            private static ConcurrentBag<string> _changes = new ConcurrentBag<string>();

            public int ReportIntervalSeconds
            {
                get { return _reportIntervalSeconds; }
                set { 
                    _reportIntervalSeconds = value;
                    _timer.Change(value*1000, value*1000);
                }
            }
            private int _reportIntervalSeconds = 3;

            public FolderSpy(string targetFolder)
            {
                _fileSystemWatcher = new FileSystemWatcher(targetFolder);
                _fileSystemWatcher.Changed += DirectoryChanged;
                _fileSystemWatcher.EnableRaisingEvents = true;
                _timer.Change(_reportIntervalSeconds * 1000, _reportIntervalSeconds * 1000);
            }

            private static void TimeTicks(object state)
            {
                var changesArray = new string[0];
                lock (Lockable)
                {
                    if (_changes.Count > 0)
                    {
                        changesArray = _changes.ToArray();
                        _changes = new ConcurrentBag<string>();
                    }
                }
                if (changesArray.Length == 0) return;
                
                //TODO: Actualy update the database instead of console dumping
                foreach (string changed in changesArray)
                    Console.WriteLine(changed);
            }

            private void DirectoryChanged(object sender, FileSystemEventArgs fileSystemEventArgs)
            {   
                ReportChange(fileSystemEventArgs.FullPath);
            }

            private void ReportChange(string changePath)
            {
                if (!changePath.ToLower().EndsWith(".txt")) return;
                lock (Lockable)
                {
                    if (_changes.Contains(changePath)) return;
                    _changes.Add(changePath);
                }
            }
            
            public void Dispose()
            {
                Dispose(true);
            }

            private void Dispose(bool disposing)
            {
                if (!disposing) return;
                
                if (_fileSystemWatcher != null)
                {
                    _fileSystemWatcher.Dispose();
                    _fileSystemWatcher = null;
                }
                if (_timer != null)
                {
                    _timer.Change(Timeout.Infinite, Timeout.Infinite);
                    _timer.Dispose();
                    _timer = null;
                }
            }
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900