public static FilenameInfo Parse(string filePath) { FilenameInfo fi = new FilenameInfo(); try { fi.filePath = filePath; fi.filename = Path.GetFileNameWithoutExtension(filePath); fi.extention = Path.GetExtension(filePath); fi.fullDirectory = Path.GetDirectoryName(filePath); string[] tmp = fi.fullDirectory.Split('\\'); fi.depth = tmp.Length - 1; fi.lastDirectoryName = tmp[tmp.Length - 1]; } catch { return(null); } return(fi); }
/// <summary> /// 处理文件函数,包括处理前后的主要业务逻辑。 /// </summary> /// <param name="infile"></param> /// <returns></returns> public virtual bool ProcessFile(string infile) { // 如果文件不存在或后缀名不匹配,就直接忽略,不产生任何记录。 if (!File.Exists(infile) || pd.extentions.IndexOf(Path.GetExtension(infile).ToLower()) < 0) { return(false); } //当前正在转换的文件。 CurrentInFile = infile; // 转换数据 TransLog log = null; try { if (TransferFile(infile)) { log = new TransLog(infile, LastOutputDfqFile, "转换成功", LogType.Success); } else { log = new TransLog(infile, LastOutputDfqFile, "转换失败,原因:未定义。", LogType.Unknown); } LogList.Add(log); } catch (Exception ex) { log = new TransLog(infile, LastOutputDfqFile, "转换失败,原因:" + ex.Message, LogType.Fail); LogList.Add(log); } // invoke the TransFileComplete event. Note ? denotes nullable. TransFileComplete.Invoke(this, log); // Processes input file. switch (pd.ProcessSourceFileType) { // move inputfile ==> backup folder. case 0: // the fileinfo of input file. FilenameInfo fi = FilenameInfo.Parse(infile); // select the backup folder according to the processing result. string backupFolder = log.LogType == LogType.Success ? pd.FolderForSuccessed : pd.FolderForFailed; // create backup directory. Directory.CreateDirectory(backupFolder); if (!Directory.Exists(backupFolder)) { LogList.Add(new TransLog(infile, backupFolder, "备份文件夹'" + backupFolder + "' 创建失败。", LogType.Backup)); break; } string outfile = null; switch (pd.KeepBackupFolderStructType) { case 0: // construct outputfile with a unique file name. outfile = FileHelper.AddIncreamentId(backupFolder + "\\" + fi.Filename + fi.Extention); break; case 1: outfile = FileHelper.GetOutFolder(infile, currentInputPath.Type == 0 ? "" : currentInputPath.path, backupFolder); Directory.CreateDirectory(Path.GetDirectoryName(outfile)); break; } // copy file. If failed, add the error to logs. if (!FileHelper.CopyFile(infile, outfile)) { LogList.Add(new TransLog(infile, outfile, String.Format("复制文件 '{0}' 至 '{1}' 失败。", infile, outfile), LogType.Backup)); break; } // delete source file. if (!FileHelper.DeleteFile(infile)) { LogList.Add(new TransLog(infile, outfile, String.Format("文件 {0} 删除失败。", infile), LogType.Backup)); } break; case 1: // no change. break; case 2: // delete files. FileHelper.DeleteFile(infile); break; case 3: // customized. break; } return(log != null && log.LogType == LogType.Success); }