0

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.

  • for reading lines in a file: http://stackoverflow.com/questions/119559/determine-the-number-of-lines-within-a-text-file As far as running every n seconds, you just call your parsing function after your specified time. For inserting into db, enumerate through your file list and for each record insert/update an entry. This seems like a somewhat broad question so it's kind of hard to tell where you need help. Additionally, what are you doing to stop (or do you care) about the fact you'll be doing duplicate work almost constantly while checking for num of lines in files that are already loaded – Kritner Jul 24 '14 at 19:40

1 Answers1

1

Depending on your actual needs, a combination of FileSystemWatcher (http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher%28v=vs.110%29.aspx) and File.ReadAllText (http://msdn.microsoft.com/en-us/library/ms143368%28v=vs.110%29.aspx) might provide you the means to achieve you goal.

FileSystemWatcher can be used to monitor a directory for changes, including new files and modifications of existing files.

You can then subscribe to the event that suits your needs, and then you can get the file name inside of the delegate.

Using File.ReadAllText allows you to easily load file contents in a string, that you should be able to include in your SQL query, either by concatenation if you are using raw SQL, or by assigning it to the relevant property of your entity if you are using an ORM.

Note that I assume your files are plain text. If you require to store it as binary, File.ReadAllBytes will be more pertinent.

rmtz
  • 646
  • 5
  • 5