一个文件监听程序,当有大量的文件进入时如何根据入库时间的不同来判定哪些是未入库的程序
该程序主要功能是监听一个文件夹 ,当文件夹中有新文件进入时就自动读取文件中的内容并将其存入到数据库。问题是,当关闭这个监听程序时怎样记录最后一个被读入内容并存入数据库的文件,然后当下次运行时这个监听程序时,怎么自动选择被监听文件夹中在监听程序未运行期间产生的文件,并自动读取其内容存入数据库。监听程序是这样写的:
void UsingFileSystemWatcher()
{
fileSystemWatcher1.Filter = "*.txt";
fileSystemWatcher1.Created += new FileSystemEventHandler(fileSystemWatcher1_Created);
}
private void btnStart_Click(object sender, EventArgs e)
{
fileSystemWatcher1.Path = folderBrowserDialog1.SelectedPath ;
fileSystemWatcher1.EnableRaisingEvents = true;
this.fileSystemWatcher1.Filter = "*.txt";
this.fileSystemWatcher1.EndInit();
}
public void fileSystemWatcher1_Created(object sender, System.IO.FileSystemEventArgs e)
{
if (e.ChangeType == WatcherChangeTypes.Created)
{
listBox1.Items.Add("日志文件:" + e.FullPath + "被创建");
string fullPath = e.FullPath;
string fileName = fullPath.Substring(fullPath.LastIndexOf("\\") + 1);
string fileFolder = fullPath.Substring(0, fullPath.LastIndexOf("\\") + 1);
string filePath = fileFolder.Substring(fileFolder.LastIndexOf('\\') + 1) + "\\" + fileName;
string file = fileFolder + fileName;
textBox1.Text = file;
string strcon = "persist Security Info = False;Initial Catalog=Audiomonitor;";
strcon += "Data Source = localhost;Integrated Security = SSPI;";
SqlConnection conn = new SqlConnection(strcon);
conn.Open();
try
{
SqlCommand com = new SqlCommand("BULK INSERT siteinformation FROM '" + file + "' WITH (FIELDTERMINATOR = ',',ROWTERMINATOR= '=')", conn);
com.ExecuteNonQuery();
MessageBox.Show("数据导入成功");
}
catch (SqlException SQLexc)
{
MessageBox.Show("导入数据库时出错:" + SQLexc.ToString());
}
conn.Close();
}
思维有点混乱,希望高手解答一下