8,834
社区成员
发帖
与我相关
我的任务
分享
FileSystemWatcher fsw = new FileSystemWatcher();
fsw.Path = ReadFilePath;
fsw.IncludeSubdirectories = false;
fsw.Filter = "*.txt";
fsw.NotifyFilter = NotifyFilters.FileName | NotifyFilters.DirectoryName | NotifyFilters.Size;
fsw.Created += new FileSystemEventHandler(this.fileSystemWatcher_EventHandle); //因为只需要对新增文件做处理,所以只添加了新增事件。
fsw.EnableRaisingEvents = true;
private void fileSystemWatcher_EventHandle(object sender, FileSystemEventArgs e)
{
if (e.ChangeType == WatcherChangeTypes.Created)
{
SaveFileToDataBase();
}
}
private void SaveFileToDataBase()
{
DirectoryInfo TheFolder = new DirectoryInfo(ReadFilePath);
string pgd_vou_no = "", do_date = "", do_time = "", opr_usr_id = "", MTID = "", SN = "", PBcount = "", OK_flag = "";
string fileName = "", bakfile = "";
foreach (FileInfo NextFile in TheFolder.GetFiles())
{
fileName = NextFile.Name.ToString().Trim();
fileName = fileName.Substring(fileName.Length - 16, 12);
StreamReader sr = new StreamReader(NextFile.FullName, Encoding.Default);
string OneLineTxt;
int i = 0;
while ((OneLineTxt = sr.ReadLine()) != null)
{
i++;
string[] SplitTxt = OneLineTxt.Split(new char[] { ',' });
pgd_vou_no = SplitTxt[0].Trim();
do_date = SplitTxt[1].Trim();
do_time = SplitTxt[2].Trim();
//do_time = SplitTxt[1].Trim() + " " + SplitTxt[2].Trim();
opr_usr_id = SplitTxt[3].Trim();
MTID = SplitTxt[4].Trim();
SN = SplitTxt[5].Trim();
PBcount = SplitTxt[6].Trim();
OK_flag = SplitTxt[7].Trim();
string up_date = " m_item_stock_no='" + fileName.Trim() + "',ok_flag='" + OK_flag + "',do_time='" + do_time + "',usr_id='" + opr_usr_id.Trim() + "',PBcount='" + PBcount.Trim() + "',MTID='" + MTID + "'";
try
{
string up_flag = srv2.One_line_update("gl_MES_SMT_print", up_date, new string[] { "com_id", "vou_no", "item_stock_no" }, new string[] { cmps, pgd_vou_no, SN }, "update", "3");
}
catch (Exception ee)
{
MessageBox.Show(ee.ToString().Trim());
}
}
sr.Dispose();
bakfile = BakFilePath + "\\" + NextFile.Name;
if (File.Exists(bakfile))
{
File.Delete(bakfile);
}
NextFile.MoveTo(bakfile);
}
}