protected override void Dispose(bool disposing) { if (disposing) { db.Dispose(); } base.Dispose(disposing); }
private string FolderRead(long DocPathID, string RealityPath, string FolderName) { var DirInfo = new DirectoryInfo(RealityPath); var EF = new EF(); //获取当前目录名记录 DocFolder DocFolder = null; if (FolderName == null) { //根目录 DocFolder = new DocFolder { ID = 0 }; } else { DocFolder = EF.DocFolder.Where(i => i.Name == FolderName).FirstOrDefault(); if (DocFolder == null) { EF.DocFolder.Add(DocFolder = new DocFolder { Name = FolderName, CreationTime = DirInfo.CreationTime, LastAccessTime = DirInfo.LastAccessTime }); EF.SaveChanges(); } else if (DocFolder.LastAccessTime == DirInfo.LastAccessTime) { //记录已经存在并且未变更,无需处理 return(null); } } //历遍子目录 foreach (DirectoryInfo Folder in DirInfo.GetDirectories()) { FolderRead(DocPathID, Folder.FullName, FolderName + Folder.Name + "/"); } //历遍文件 foreach (var iFile in DirInfo.GetFiles()) { var DocFile = EF.DocFile.Where(i => i.DocFolderID == DocFolder.ID && i.Name == iFile.Name).FirstOrDefault(); if (DocFile == null) { EF.DocFile.Add(DocFile = new DocFile { DocPathID = DocPathID, DocFolderID = DocFolder.ID, Name = iFile.Name, ExtNameID = SYS.DOC.GetFileExtName(iFile.Extension).ID, Length = iFile.Length, MIMEID = SYS.DOC.GetFileMIME(Utils.GetMime(Path.GetExtension(iFile.FullName).ToLower())).ID, CreationTime = iFile.CreationTime, LastAccessTime = iFile.LastWriteTime }); try { EF.SaveChanges(); } catch (DbEntityValidationException ex) { string error = ""; foreach (var item in ex.EntityValidationErrors) { foreach (var item2 in item.ValidationErrors) { error += string.Format("{0}:{1}\r\n", item2.PropertyName, item2.ErrorMessage); } } throw new Exception(error); } catch (Exception) { throw; } } else if (DocFile.LastAccessTime == iFile.LastAccessTime) { //记录已经存在并且未变更,无需处理 break; } //异步创建预览 FileSimpleHandler tFileSimpleHandler = new FileSimpleHandler(FileSimpleCreate); IAsyncResult asyncResult = tFileSimpleHandler.BeginInvoke(DocFile.ID, new AsyncCallback(FileSimpleCallback), DocFile); new LogManager().WriteLog("文件[" + DocFile.ID + "]创建预览开始。"); } EF.Dispose(); return(null); }