示例#1
0
        static void Main(string[] args)
        {
            //Create a new FileSystemWatcher.

            int port = int.Parse(args[1]);
            int option = int.Parse(args[0]);
            if (option == 1)
            {

                try
                {

                    socket = new TcpClient("localhost", port);
                    Console.WriteLine("connected to server");
                }
                catch
                {
                    Console.WriteLine(
                    "Failed to connect to server at {0}", port);

                    Console.Read();
                    return;
                }
                NetworkStream networkStream = socket.GetStream();
                System.IO.StreamReader streamReader = new System.IO.StreamReader(networkStream);
                writer = new System.IO.StreamWriter(networkStream);
                try
                {
                    string outputString;
                    // read the data from the host and display it

                    outputString = streamReader.ReadLine();
                    Console.WriteLine(outputString);
                    Console.WriteLine("message received");

                    // tidy up

                    FileSystemWatcher watcher = new FileSystemWatcher();

                    //Set the filter to only catch TXT files.

                    //Subscribe to the Created event.
                    watcher.Created += new FileSystemEventHandler(watcher_Created2);

                    //Set the path to C:\Temp\
                    watcher.Path = @outputString;

                    //Enable the FileSystemWatcher events.
                    watcher.EnableRaisingEvents = true;
                    for (; ; ) ;
                }
                catch
                {
                    Console.WriteLine("Exception reading from Server");
                }
            }
            else if (option == 2)
            {

                try
                {

                    socket = new TcpClient("localhost", port);
                    Console.WriteLine("connected to server");
                }
                catch
                {
                    Console.WriteLine(
                    "Failed to connect to server at {0}:999", "localhost");
                    return;
                }
                NetworkStream networkStream = socket.GetStream();
                System.IO.StreamReader streamReader = new System.IO.StreamReader(networkStream);
                writer = new System.IO.StreamWriter(networkStream);
                try
                {
                    string outputString;
                    // read the data from the host and display it

                    outputString = streamReader.ReadLine();
                    Console.WriteLine(outputString);
                    Console.WriteLine("message received");

                    // tidy up

                    FileSystemWatcher watcher = new FileSystemWatcher();

                    //Set the filter to only catch TXT files.

                    //Subscribe to the Created event.
                    watcher.Created += new FileSystemEventHandler(watcher_Created1);

                    //Set the path to C:\Temp\
                    watcher.Path = @outputString;

                    //Enable the FileSystemWatcher events.
                    watcher.EnableRaisingEvents = true;
                    for (; ; ) ;
                }
                catch
                {
                    Console.WriteLine("Exception reading from Server");
                }

            }
            else
            {
                fw = new FileSystemWatcher();
                //fw.Path = @path1;
                path1 = args[2];
                Thread.Sleep(40000);
                fw.Path = @path1 + "\\METADATA";
                //Console.WriteLine("Path set");
                fw.Filter = "*_METS.xml";
                //Console.WriteLine("filter set");
                fw.EnableRaisingEvents = true;
                //Console.WriteLine("events enabled");
                filewatcher filew = new filewatcher(fw, path1, writer);

                Thread thread = new Thread(new ThreadStart(filew.WorkThreadFunction));
                thread.Start();

            }
        }
示例#2
0
        static void watcher_Created2(object sender, FileSystemEventArgs e)
        {
            try
            {
                path1 = e.FullPath;
                Console.WriteLine(fw);
                //fw.Created -= new FileSystemEventHandler(watcher_Created2);
                Console.WriteLine(path1);
                writer.WriteLine(path1);
                writer.Flush();
                fw = new FileSystemWatcher();
                fw.Path = @path1;
                //Thread.Sleep(20000);
                //fw.Path = @path1 + "\\METADATA";
                //Console.WriteLine("Path set");
                //fw.Filter = "*_METS.xml";
                //Console.WriteLine("filter set");
                fw.EnableRaisingEvents = true;
                //Console.WriteLine("events enabled");
                filewatcher filew = new filewatcher(fw, path1, writer);
                fw.Created += new FileSystemEventHandler(watcher_Created1);

                //Thread thread = new Thread(new ThreadStart(filew.WorkThreadFunction));
                //thread.Start();

            }
            catch (Exception ee)
            {
                Console.WriteLine("error in the handler for directory {0}", ee);
            }
        }