程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#采取FileSystemWatcher完成監督磁盤文件變革的辦法

C#采取FileSystemWatcher完成監督磁盤文件變革的辦法

編輯:C#入門知識

C#采取FileSystemWatcher完成監督磁盤文件變革的辦法。本站提示廣大學習愛好者:(C#采取FileSystemWatcher完成監督磁盤文件變革的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#采取FileSystemWatcher完成監督磁盤文件變革的辦法正文


本文實例講述了C#采取FileSystemWatcher完成監督磁盤文件變革的辦法。分享給年夜家供年夜家參考。詳細完成辦法以下:

簡化需求:有一個簡化了的需求是如許的:有一個攝影法式在運轉,一旦抓拍以後則將圖片文件存儲至某目次,然後圖片要上傳至長途辦事器並update數據庫。

原需求:本來的需求是如許的:有一台PDA掃碼槍,一個IP拍照機放置鄙人線區傳送帶上方。當PDA掃描箱子上的條碼,觸發相機攝影,將圖片傳播至遠端辦事器,找到對應的條碼,將圖片存儲並更新數據庫。

但是我不曉得PDA掃描的剎時若何與IP相機通訊(藍牙或WLAN?),其實症結是我不曉得如何應用IP相機的外觸發功效,增長藍牙觸發器?也不曉得如何hack或ssh到這個相機(應當是linux的吧),所以只能先應用簡化需求的版本。

而簡化需求的版本,症結就是監督文件夾內容變更與上傳文件流。

昨天問了下度娘,C#中的監督組件名字叫做FileSystemWatcher。

因而寫了個demo,可以監督一切邏輯盤或許某個文件夾。

應用辦法:

1.直接翻開是監督一切邏輯磁盤文件變更。

2.或許傳遞參數,監督某一途徑文件變更。如圖,監督e盤

源代碼以下:


namespace FileSystemWatcherDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //watcher組
            FileSystemWatcher[] watchers;

            //若未傳遞參數,則監督一切文件體系,包含CD-ROM(弗成用),可挪動磁盤(弗成用)等
            if (args.Length == 0)
            {
                string[] drivers = Directory.GetLogicalDrives();
                watchers = new FileSystemWatcher[drivers.Length];

                for (int i = 0; i < drivers.Length; i++)
                {
                    try
                    {
                        watchers[i] = new FileSystemWatcher { Path = drivers[i] };
                    }
                    catch (Exception ex)
                    {
                        Trace.TraceWarning(ex.Message);
                    }
                }
            }
            else
            {
                watchers = new FileSystemWatcher[1];
                watchers[0] = new FileSystemWatcher { Path = args[0] };
            }

            foreach (FileSystemWatcher w in watchers)
            {
                if (w == null) continue;

                w.Filter = "*";
                w.IncludeSubdirectories = true;
                w.EnableRaisingEvents = true;

                w.Created += onFileSystem_Changed;
                w.Deleted += onFileSystem_Changed;
                w.Changed += onFileSystem_Changed;
                w.Renamed += watcher_Renamed;
            }

            Console.ReadLine();
        }

        #region [ 檢測文件能否占用 ]
        /// <summary>
        /// 檢測文件能否占用
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        static bool IsFileReady(string filename)
        {
            var fi = new FileInfo(filename);
            FileStream fs = null;
            try
            {
                fs = fi.Open(FileMode.Open, FileAccess.Read, FileShare.None);
                return true;
            }
            catch (IOException)
            {
                return false;
            }

            finally
            {
                if (fs != null)
                    fs.Close();
            }
        }
        #endregion

        private static volatile object _lock = true;
        static void onFileSystem_Changed(object sender, FileSystemEventArgs e)
        {
            lock (_lock)
            {
                Console.ForegroundColor = ConsoleColor.DarkGray;
                Console.Write("[");
                Console.Write(DateTime.Now.ToString("HH:mm:ss"));
                Console.Write("] ");

                switch (e.ChangeType.ToString().ToLower())
                {
                    case "created":
                        //while (!IsFileReady(e.FullPath))
                        //{
                        //    if (!File.Exists(e.FullPath))
                        //        return;
                        //    Thread.Sleep(100);
                        //}
                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.Write(e.ChangeType);
                        Console.ForegroundColor = ConsoleColor.White;
                        Console.Write(" ");
                        Console.Write(e.Name);
                        Console.Write(" ");
                        Console.ForegroundColor = ConsoleColor.DarkGray;
                        Console.Write(e.FullPath);

                        break;
                    case "deleted":
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.Write(e.ChangeType);
                        Console.ForegroundColor = ConsoleColor.White;
                        Console.Write(" ");
                        Console.Write(e.Name);
                        Console.Write(" ");
                        Console.ForegroundColor = ConsoleColor.DarkGray;
                        Console.Write(e.FullPath);
                        break;
                    case "changed":
                        Console.ForegroundColor = ConsoleColor.Cyan;
                        Console.Write(e.ChangeType);
                        Console.ForegroundColor = ConsoleColor.White;
                        Console.Write(" ");
                        Console.Write(e.Name);
                        Console.Write(" ");
                        Console.ForegroundColor = ConsoleColor.DarkGray;
                        Console.Write(e.FullPath);
                        break;
                }

                Console.Write("\r\n");
            }
        }
        static void watcher_Renamed(object sender, RenamedEventArgs e)
        {
            Console.ForegroundColor = ConsoleColor.Magenta;
            Console.Write(e.ChangeType);
            Console.ForegroundColor = ConsoleColor.White;
            Console.Write(" ");
            Console.Write(e.OldName);
            Console.Write(e.OldFullPath);
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.Write(" ");
            Console.Write(e.Name);
            Console.Write(e.FullPath);
            Console.Write(Thread.CurrentThread.Name);
            Console.Write("\r\n");
        }
    }
}

願望本文所述對年夜家的C#法式設計有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved