亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C#采用FileSystemWatcher實(shí)現(xiàn)監(jiān)視磁盤(pán)文件變更的方法

 更新時(shí)間:2014年11月18日 10:46:05   投稿:shichen2014  
這篇文章主要介紹了C#采用FileSystemWatcher實(shí)現(xiàn)監(jiān)視磁盤(pán)文件變更的方法,詳細(xì)分析了FileSystemWatcher的用法,并以此為基礎(chǔ)實(shí)現(xiàn)監(jiān)視磁盤(pán)文件變更,是非常實(shí)用的技巧,具有一定的借鑒價(jià)值,需要的朋友可以參考下

本文實(shí)例講述了C#采用FileSystemWatcher實(shí)現(xiàn)監(jiān)視磁盤(pán)文件變更的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:

簡(jiǎn)化需求:有一個(gè)簡(jiǎn)化了的需求是這樣的:有一個(gè)拍照程序在運(yùn)行,一旦抓拍之后則將圖片文件存儲(chǔ)至某目錄,然后圖片要上傳至遠(yuǎn)程服務(wù)器并update數(shù)據(jù)庫(kù)。

原需求:原先的需求是這樣的:有一臺(tái)PDA掃碼槍?zhuān)粋€(gè)IP照相機(jī)放置在下線(xiàn)區(qū)傳送帶上方。當(dāng)PDA掃描箱子上的條碼,觸發(fā)相機(jī)拍照,將圖片流傳至遠(yuǎn)端服務(wù)器,找到對(duì)應(yīng)的條碼,將圖片存儲(chǔ)并更新數(shù)據(jù)庫(kù)。

然而我不知道PDA掃描的瞬間如何與IP相機(jī)通信(藍(lán)牙或WLAN?),其實(shí)關(guān)鍵是我不知道怎樣使用IP相機(jī)的外觸發(fā)功能,增加藍(lán)牙觸發(fā)器?也不知道怎樣hack或ssh到這個(gè)相機(jī)(應(yīng)該是linux的吧),所以只能先使用簡(jiǎn)化需求的版本。

而簡(jiǎn)化需求的版本,關(guān)鍵就是監(jiān)視文件夾內(nèi)容變化與上傳文件流。

昨天問(wèn)了下度娘,C#中的監(jiān)視組件名字叫做FileSystemWatcher。

于是寫(xiě)了個(gè)demo,可以監(jiān)視所有邏輯盤(pán)或者某個(gè)文件夾。

使用方法:

1.直接打開(kāi)是監(jiān)視所有邏輯磁盤(pán)文件變化。

2.或者傳遞參數(shù),監(jiān)視某一路徑文件變化。如圖,監(jiān)視e盤(pán)

源代碼如下:

復(fù)制代碼 代碼如下:

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

            //若未傳遞參數(shù),則監(jiān)視所有文件系統(tǒng),包括CD-ROM(不可用),可移動(dòng)磁盤(pán)(不可用)等
            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 [ 檢測(cè)文件是否占用 ]
        /// <summary>
        /// 檢測(cè)文件是否占用
        /// </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");
        }
    }
}

希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論