i have a filesystemwatcher looking for a file, then once the file comes into the folder ive set, it then reads all the lines of data from inside each file in the folder being monitored. each read line should then be writen into another file in a different folder. so i have one file full of all the data from the files in the monitored folder!
here is the code i have so far:
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
public static void Run()
{
string[] args = System.Environment.GetCommandLineArgs();
// Create a new FileSystemWatcher and set its properties.
FileSystemWatcher watcher = new FileSystemWatcher();
string test1 = System.Configuration.ConfigurationSettings.AppSettings["DestDir"];
watcher.Path = test1;//args[1];
/* 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 = "1.*";
// 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.lol
Console.WriteLine("Press \'q\' to quit the sample.");
while (Console.Read() != 'q') ;
}
// Define the event handlers.
private static void OnChanged(object source, FileSystemEventArgs e)
{
File.Delete(e.FullPath);
//string[] files = System.IO.Directory.GetFiles(Path.GetDirectoryName(e.FullPath));
DirectoryInfo dr = new DirectoryInfo(Path.GetDirectoryName(e.FullPath));
FileInfo[] files1 = dr.GetFiles("*.*");
foreach (FileInfo file in files1)
{
try
{
using (FileStream fs = new FileStream(file.FullName, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
{
// Create an instance of StreamReader to read from a file.
// The using statement also closes the StreamReader.
using (StreamReader sr = new StreamReader(fs))
{
String line;
// Read and display lines from the file until the end of
// the file is reached.
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
// add streamwrite to put each line into another file
using (StreamWriter sw = new StreamWriter(@"C:\Documents and Settings\chris.kennedy\Desktop\process\Work.txt"))
{
//foreach (FileInfo file in files1)
// {
sw.WriteLine("\n");
sw.WriteLine(line + "\n");
Console.WriteLine("im doing it ok");
// }
}
the sw.writeline appears only to write the last line of the last file, hence the \n to make it return , so that no overwriting goes on. but that doesnt work either, i also checked that the files in the monotored folder had no spaces or returns after the data to make sure that didnt affect it.