首页 > 代码库 > .Net 监控文件夹是否有新文件生成,并确认文件没有被其他程序占用

.Net 监控文件夹是否有新文件生成,并确认文件没有被其他程序占用

监控文件夹测试程序:

 

技术分享
 1 using System;
 2 using System.Collections.Generic;
 3 using System.IO;
 4 using System.Linq;
 5 using System.Text;
 6 using System.Threading;
 7 using System.Threading.Tasks;
 8 
 9 namespace FileSystemWatcherTest
10 {
11     class Program
12     {
13         static void Main(string[] args)
14         {
15             FileSystemWatcher watcher = new FileSystemWatcher("C:\\FileSystemWatcher", "*.txt");
16             watcher.NotifyFilter = NotifyFilters.FileName;
17             watcher.Created += new FileSystemEventHandler(FileCreated);
18             watcher.EnableRaisingEvents = true;
19 
20             Console.ReadLine();
21         }
22 
23         private static void FileCreated(object sender, FileSystemEventArgs e)
24         {
25             if (!File.Exists(e.FullPath))
26             {
27                 return;
28             }
29             Console.WriteLine("Created: {0: HH:mm:ss}", DateTime.Now);
30 
31             while (!IsFileReady(e.FullPath))
32             {
33                 Console.WriteLine("Used: {0: HH:mm:ss}", DateTime.Now);
34             }
35             //在这里进行文件处理。。。  
36             Console.WriteLine("Ready: {0: HH:mm:ss}", DateTime.Now);
37 
38             Thread.Sleep(1000 * 5);
39             FileInfo fs = new FileInfo(e.FullPath);
40 
41             var moveToPath = @"\\testServer\Shares\" + fs.Name;
42             fs.MoveTo(moveToPath);
43 
44             if (!File.Exists(moveToPath))
45             {
46                 Console.WriteLine("Move Faild: {0: HH:mm:ss}", DateTime.Now);
47             }
48             Console.WriteLine("Move Success: {0: HH:mm:ss}", DateTime.Now);
49         }
50 
51         static bool IsFileReady(string filename)
52         {
53             FileInfo fi = new FileInfo(filename);
54             FileStream fs = null;
55             try
56             {
57                 fs = fi.Open(FileMode.Open, FileAccess.ReadWrite,FileShare.None);
58                 return true;
59             }
60 
61             catch (IOException)
62             {
63                 return false;
64             }
65 
66             finally
67             {
68                 if (fs != null)
69                     fs.Close();
70             }
71         }
72     }
73 }
View Code

 

 

文件生成测试程序:

技术分享
 1 using System;
 2 using System.Collections.Generic;
 3 using System.IO;
 4 using System.Linq;
 5 using System.Text;
 6 using System.Threading;
 7 using System.Threading.Tasks;
 8 
 9 namespace GenerateFileTest
10 {
11     class Program
12     {
13         static void Main(string[] args)
14         {
15             FileStream fs = new FileStream("C:\\FileSystemWatcher\\Test.txt", FileMode.Create);
16             StreamWriter sw = new StreamWriter(fs);
17 
18             var currDateTime = DateTime.Now;
19             var maxDateTime = currDateTime.AddSeconds(10);
20 
21             Console.WriteLine("Log begin: {0}", DateTime.Now.ToString("HH:mm:ss"));
22             while (currDateTime < maxDateTime)
23             {
24                 sw.WriteLine(DateTime.Now.ToString());
25                 currDateTime = DateTime.Now;
26             }
27 
28             //清空缓冲区
29             sw.Flush();
30             //关闭流
31             sw.Close();
32             fs.Close();
33 
34             Console.WriteLine("Log end: {0}", DateTime.Now.ToString("HH:mm:ss"));
35             Console.ReadLine();
36         }
37     }
38 }
View Code

 

运行结果:
技术分享

 

 

 

.Net 监控文件夹是否有新文件生成,并确认文件没有被其他程序占用