/// <summary> /// 监视文件 /// </summary> private void WacthFile(string filePath, DateTime oldDt) { if (!txtfilename.Text.Equals(filePath)) { return; } FileInfo fi = new FileInfo(FilePath + "\\" + filePath + ".rtf"); DateTime dt = fi.LastWriteTime; TimeSpan span = oldDt.Subtract(dt); if (span.Seconds < 0) { this.Invoke(new Action(() => { IntPtr vHandle = _lopen(FilePath + "\\" + filePath + ".rtf", OF_READWRITE | OF_SHARE_DENY_NONE); if (vHandle == HFILE_ERROR) { return; } else { CloseHandle(vHandle); //判断之后一定要关闭!!! oldDt = dt; System.Console.WriteLine(dt); ContentRTB.LoadFile(FilePath + "\\" + filePath + ".rtf", RichTextBoxStreamType.RichText); } })); } Console.WriteLine("WacthFile" + DateTime.Now.ToString()); fi = null; Thread.Sleep(1000); //如果当前编辑的文件和当前处理的文件一致的时候 WacthFile(filePath, oldDt); }
private void Planlist_DoubleClickWork2(Record item, EventArgs e) { this.Invoke(new Action(() => { //loadpc.Visible = true; ProcessBarStart(); txtcompor.Text = item.Compor.ToLower(); txtid.Text = item.Id.ToString(); txtfilename.Text = item.Filename.ToString(); txttitle.Text = item.Title.ToString(); txthour.Text = item.Hour.ToString(); cbstatus.SelectedIndex = int.Parse((item.Status.ToString() == ""?"0": item.Status.ToString())); cbprocessor.SelectedValue = item.Processor.ToString(); txtremark.Text = item.Remark.ToString(); estimateDate.Text = item.Estimate.ToString(); txtrelor.Text = item.Relor.ToString(); if (File.Exists(FilePath + "\\" + item.Filename.ToString() + ".rtf")) { IntPtr vHandle = _lopen(FilePath + "\\" + item.Filename.ToString() + ".rtf", OF_READWRITE | OF_SHARE_DENY_NONE); if (vHandle == HFILE_ERROR) { ShowMessageDialog("文件被占用"); ContentRTB.Text = ""; return; } CloseHandle(vHandle); //判断之后一定要关闭!!! FileInfo fi = new FileInfo(FilePath + "\\" + item.Filename.ToString() + ".rtf"); DateTime dt = fi.LastWriteTime; DataSet ds = MySqlHelper.ExecuteSQL("select * from pasn where id= " + txtid.Text); DateTime dt1 = Convert.ToDateTime(ds.Tables[0].Rows[0]["lastwritetime"]); TimeSpan span = dt.Subtract(dt1); //如果服务器较新,用服务器的文件,第二次再打开的时候,因为从服务器下载了,所以本地文件会更新 if (span.Seconds < 0) { var r = DownLoadFile(FileUrl + item.Filename.ToString() + ".rtf", FilePath + "\\" + item.Filename.ToString() + ".rtf", progressBar1); if (r) { ContentRTB.LoadFile(FilePath + "\\" + item.Filename.ToString() + ".rtf", RichTextBoxStreamType.RichText); } else { ShowMessageDialog("下载文件失败"); ContentRTB.Text = ""; } } else { ContentRTB.LoadFile(FilePath + "\\" + item.Filename.ToString() + ".rtf", RichTextBoxStreamType.RichText); } } else { var r = DownLoadFile(FileUrl + item.Filename.ToString() + ".rtf", FilePath + "\\" + item.Filename.ToString() + ".rtf", progressBar1); if (r) { ContentRTB.LoadFile(FilePath + "\\" + item.Filename.ToString() + ".rtf", RichTextBoxStreamType.RichText); } else { ShowMessageDialog("下载文件失败"); ContentRTB.Text = ""; } } //this.loadpc.Visible = false; ProcessBarStop(); })); }
private void Planlist_DoubleClickWork(ListView l, EventArgs e) { this.Invoke(new Action(() => { DataRow item = null; loadpc.Visible = true; item = (DataRow)(l.SelectedItems[0]).Tag; txtid.Text = item["id"].ToString(); txtfilename.Text = item["filename"].ToString(); txttitle.Text = item["title"].ToString(); txthour.Text = item["hour"].ToString(); cbstatus.SelectedIndex = int.Parse(item["status"].ToString()); cbprocessor.SelectedValue = item["processor"].ToString(); txtremark.Text = item["remark"].ToString(); estimateDate.Text = item["estimate"].ToString(); txtrelor.Text = item["relor"].ToString(); if (File.Exists(FilePath + "\\" + item["filename"].ToString() + ".rtf")) { IntPtr vHandle = _lopen(FilePath + "\\" + item["filename"].ToString() + ".rtf", OF_READWRITE | OF_SHARE_DENY_NONE); if (vHandle == HFILE_ERROR) { MessageBoxEx.Show(this, "文件被占用"); ContentRTB.Clear(); return; } CloseHandle(vHandle); //判断之后一定要关闭!!! FileInfo fi = new FileInfo(FilePath + "\\" + item["filename"].ToString() + ".rtf"); DateTime dt = fi.LastWriteTime; DataSet ds = MySqlHelper.ExecuteSQL("select * from pasn where id= " + txtid.Text); DateTime dt1 = Convert.ToDateTime(ds.Tables[0].Rows[0]["lastwritetime"]); TimeSpan span = dt.Subtract(dt1); //如果服务器较新,用服务器的文件,第二次再打开的时候,因为从服务器下载了,所以本地文件会更新 if (span.Seconds < 0) { var r = DownLoadFile(FileUrl + item["filename"].ToString() + ".rtf", FilePath + "\\" + item["filename"].ToString() + ".rtf", progressBar1); if (r) { ContentRTB.LoadFile(FilePath + "\\" + item["filename"].ToString() + ".rtf", RichTextBoxStreamType.RichText); } else { MessageBoxEx.Show(this, "下载文件失败"); ContentRTB.Clear(); } } else { ContentRTB.LoadFile(FilePath + "\\" + item["filename"].ToString() + ".rtf", RichTextBoxStreamType.RichText); } } else { var r = DownLoadFile(FileUrl + item["filename"].ToString() + ".rtf", FilePath + "\\" + item["filename"].ToString() + ".rtf", progressBar1); if (r) { ContentRTB.LoadFile(FilePath + "\\" + item["filename"].ToString() + ".rtf", RichTextBoxStreamType.RichText); } else { MessageBoxEx.Show(this, "下载文件失败"); ContentRTB.Clear(); } } this.loadpc.Visible = false; })); }