我们都知道filesystemwatcher有个Created 的event, 一般情况下我们直接handle created event就可以直接access 那个文件了, 可是当碰到大文件时就有问题了, 因为文件还在copying. 所以不能对文件做任何operation. 那怎么办呢? 这是我很久以前给朋友写的一个小程序,可以解决这个问题.
1. Create a new FileSystemWatcher.
2. Write Code to handle File Created Event.
3. what is in the ImportHelper Class.
1. Create a new FileSystemWatcher.
1
System.IO.FileSystemWatcher fswXmlFileWatcher = new System.IO.FileSystemWatcher();
2
this.fswXmlFileWatcher.EnableRaisingEvents = true;
3
this.fswXmlFileWatcher.Path = @"C:\Test"
4
//in here I only handle the file created event.
5
this.fswXmlFileWatcher.Created += new System.IO.FileSystemEventHandler(this.fswXmlFileWatcher_Created);
System.IO.FileSystemWatcher fswXmlFileWatcher = new System.IO.FileSystemWatcher();2
this.fswXmlFileWatcher.EnableRaisingEvents = true; 3
this.fswXmlFileWatcher.Path = @"C:\Test"4
//in here I only handle the file created event.5
this.fswXmlFileWatcher.Created += new System.IO.FileSystemEventHandler(this.fswXmlFileWatcher_Created);2. Write Code to handle File Created Event.
1
private void fswXmlFileWatcher_Created(object sender, System.IO.FileSystemEventArgs e)
2
{
3
lock(this)
4
{
5
string filePath = e.FullPath;
6
//incase the file is huge, it need some time to write the whole. so we wait untill the file is accessable by .Net
7
while (File.GetAttributes(filePath) == FileAttributes.Offline)
8
{
9
Thread.Sleep(500);
10
}
11
//Ok. We start a new thread to process the file
12
ImportHelper ih = new ImportHelper(filePath);
13
Thread th = new Thread(new ThreadStart(ih.ImportXmlFile));
14
th.Start();
15
}
16
}
17
private void fswXmlFileWatcher_Created(object sender, System.IO.FileSystemEventArgs e)2
{3
lock(this)4
{5
string filePath = e.FullPath;6
//incase the file is huge, it need some time to write the whole. so we wait untill the file is accessable by .Net7
while (File.GetAttributes(filePath) == FileAttributes.Offline)8
{9
Thread.Sleep(500);10
}11
//Ok. We start a new thread to process the file12
ImportHelper ih = new ImportHelper(filePath);13
Thread th = new Thread(new ThreadStart(ih.ImportXmlFile));14
th.Start();15
}16
} 17

3. what is in the ImportHelper Class.
1
public class ImportHelper
2
{
3
private string _filePath;
4
private long _fileSize;
5
private FileInfo _fileInfo;
6
7
public ImportHelper(string filePath)
8
{
9
this._filePath = filePath;
10
this._fileSize = 0;
11
this._fileInfo = new FileInfo(this._filePath);
12
}
13
14
//we need to this method. same reason. cause the file size is huge.
15
private bool TestFile()
16
{
17
long size = this._fileInfo.Length;
18
if( this._fileSize == size )
19
{
20
return true;
21
}
22
else
23
{
24
this._fileSize = size;
25
return false;
26
}
27
}
28
29
public void ImportXmlFile()
30
{
31
while( !this.TestFile() )
32
{
33
Thread.Sleep(2000);
34
}
35
//It is ok now. The file is ready for us to process.
36
//Write your own function to process the file.
37
ProcessMyFile(this._filePath)
38
}
39
}
40
public class ImportHelper2
{3
private string _filePath;4
private long _fileSize;5
private FileInfo _fileInfo;6

7
public ImportHelper(string filePath)8
{9
this._filePath = filePath;10
this._fileSize = 0;11
this._fileInfo = new FileInfo(this._filePath);12
}13

14
//we need to this method. same reason. cause the file size is huge.15
private bool TestFile()16
{17
long size = this._fileInfo.Length;18
if( this._fileSize == size )19
{20
return true;21
}22
else23
{24
this._fileSize = size;25
return false;26
}27
}28

29
public void ImportXmlFile()30
{31
while( !this.TestFile() )32
{33
Thread.Sleep(2000);34
}35
//It is ok now. The file is ready for us to process.36
//Write your own function to process the file.37
ProcessMyFile(this._filePath)38
}39
}40




浙公网安备 33010602011771号