主入口
static void Main(string[] args)
{
TestingDataChecker dataChecker = new TestingDataChecker("IBW087NB", new DateTime(2021, 5, 11), "IBW087NB-10000020");
var r = dataChecker.Check("IBW087NB-1005210016");
var r1 = dataChecker.Check("IBW087NB-1005210017");
if (r.Success)
{
dataChecker.Packing("IBW087NB-10000020");
}
}
TestingDataChecker.cs
public class TestingDataChecker { public TestingDataChecker(string project,DateTime date, string name) { Project = project; Filename = name; //DateTime.Now.ToLocalTime().ToString().Replace(":","").Replace(" ","").Replace("/",""); OringalTestingTotalResult = LoadTotalResult(); EnsureOutputPath(); //ResultColumns = new string[] { "OV_Result", "COV1_Result", "CUV1_Result", "OCCHG1_Result", "OCDSG1_Result", "NormalSCC_Result", "IRN_Result", "THR_Result" }; ResultColumns = new string[] { "Result"}; } string TestingResultRoot { get { return @"D:smt"; } } string TestingTotalResultFile { get; set; } string OutPutPath { get { return "SMTCheck"; } } string Project { get; set; } string Filename { get; set; } DataTable OringalTestingTotalResult { get; set; } DataTable OutputTestingTotalResult { get; set; } string[] ResultColumns { get;set; } void EnsureOutputPath() { var target = Path.Combine(OutPutPath + "\" + Filename, Filename); if (!Directory.Exists(target)) { Directory.CreateDirectory(target); } OutputTestingTotalResult = OringalTestingTotalResult.Clone(); var totalfile = Path.Combine(OutPutPath + "\" + Filename, new FileInfo(TestingTotalResultFile).Name.Replace("csv", "xlsx")); //.Replace("csv","xlsx") var path1 = string.Format("{0}*.xlsx", "20210511_IBW087NB"); var pathname = Path.Combine(OutPutPath + "\" + Filename); var files = Directory.GetFiles(pathname, path1); if (files.Length > 0) //判断文件是否已经存在,存在的话先把已有的记录存下来 { ExcelHelper excel = new ExcelHelper(); var table = excel.Read(files[0]); OutputTestingTotalResult = table; } else { OutputTestingTotalResult = OringalTestingTotalResult.Clone(); } } DataTable LoadTotalResult() { string sDir = @"D:\smt"; //var sDir = string.Format("{0}_{1}*.csv", Date, Project); DirectoryInfo di = new DirectoryInfo(sDir); var CreationTime = DateTime.Now.AddDays(-10000); foreach (FileInfo fi in di.GetFiles("20210511_IBW087NB*.csv")) { var a = fi.Name; var b = fi.CreationTime; } var path = string.Format("{0}\{1}.csv", "D:\smt", "20210511_IBW087NB-PCM-R0-2021-4-14"); if (path.Length > 0) { DataTable table = CSVFileHelper.OpenCSV(path); TestingTotalResultFile = path; //ExcelHelper excel = new ExcelHelper(); //var table = excel.Read(files[0]); return table; } else throw new Exception("无法找到主文件"); } public JeffSoft.OperationResult Check(string sn) { var result = new JeffSoft.OperationResult(); //var newsn="""+sn+ " ""; //发现文件中记录的数据后面有 ,不加的话查不到。 var newsn = sn + " "; //发现文件中记录的数据后面有 ,不加的话查不到。 //var rows=OringalTestingTotalResult.Select("barcode='" + newsn + "'", "StartTime desc"); var rows = OringalTestingTotalResult.Select("barcode like '%" + newsn + "%'", "StartTime desc"); if (rows.Length > 0) { bool flag = true; foreach (var c in ResultColumns) { if (rows[0][c].ToString() != "Pass") { flag = false; break; } } if (flag) { OutputTestingTotalResult.Rows.Add(rows[0].ItemArray); var starttime = Convert.ToDateTime(rows[0]["StartTime"]); var subfile = string.Format("{0}_{1}_{2}*.xls",sn, starttime.ToString("yyyy-MM-dd"), starttime.ToString("HHmm")); var files = Directory.GetFiles(Path.Combine(TestingResultRoot, "sub"), subfile); if (files.Length > 0) { File.Copy(files[0], Path.Combine(OutPutPath + "\" + Filename, Filename, new FileInfo(files[0]).Name),true); } else result.Errors.Add("找不到子文件:" + subfile); } else result.Errors.Add("存在不通过的检查项"); } else result.Errors.Add("主文件中找不到指定的条码"); return result; } /// <summary> /// 将生成的文件压缩成一个文件方便发送 /// </summary> public string Packing(string sn) { ExcelHelper helper = new ExcelHelper(); helper.DataSource = OutputTestingTotalResult; var tmp=helper.Export(); var totalfile = Path.Combine(OutPutPath + "\" + Filename, new FileInfo(TestingTotalResultFile).Name.Replace("csv", "xlsx")); //.Replace("csv","xlsx") File.Copy(tmp, totalfile,true); List<string> files = new List<string>(); List<string> zipfiles = new List<string>(); files.Add(new FileInfo(totalfile).FullName); var subfiles = Directory.GetFiles(Path.Combine(OutPutPath + "\" + Filename, Filename)); foreach(var s in subfiles) { files.Add(new FileInfo(s).FullName); } foreach(var f in files) { zipfiles.Add(f.Replace(new DirectoryInfo(OutPutPath + "\" + Filename).FullName,"")); } JeffSoft.Compress compress = new JeffSoft.Compress(); string zippath = Path.Combine(OutPutPath + "\" + Filename, string.Format("{0}.zip", Filename)); compress.CompressFile(files.ToArray(),zippath, zipfiles.ToArray()); return zippath; } }
拓展:给定文件的路径,读取文件的二进制数据,判断文件的编码类型
internal class Common { /// 给定文件的路径,读取文件的二进制数据,判断文件的编码类型 /// <param name="FILE_NAME">文件路径</param> /// <returns>文件的编码类型</returns> public static System.Text.Encoding GetType(string FILE_NAME) { System.IO.FileStream fs = new System.IO.FileStream(FILE_NAME, System.IO.FileMode.Open, System.IO.FileAccess.Read); System.Text.Encoding r = GetType(fs); fs.Close(); return r; } /// 通过给定的文件流,判断文件的编码类型 /// <param name="fs">文件流</param> /// <returns>文件的编码类型</returns> public static System.Text.Encoding GetType(System.IO.FileStream fs) { byte[] Unicode = new byte[] { 0xFF, 0xFE, 0x41 }; byte[] UnicodeBIG = new byte[] { 0xFE, 0xFF, 0x00 }; byte[] UTF8 = new byte[] { 0xEF, 0xBB, 0xBF }; //带BOM System.Text.Encoding reVal = System.Text.Encoding.Default; System.IO.BinaryReader r = new System.IO.BinaryReader(fs, System.Text.Encoding.Default); int i; int.TryParse(fs.Length.ToString(), out i); byte[] ss = r.ReadBytes(i); if (IsUTF8Bytes(ss) || (ss[0] == 0xEF && ss[1] == 0xBB && ss[2] == 0xBF)) { reVal = System.Text.Encoding.UTF8; } else if (ss[0] == 0xFE && ss[1] == 0xFF && ss[2] == 0x00) { reVal = System.Text.Encoding.BigEndianUnicode; } else if (ss[0] == 0xFF && ss[1] == 0xFE && ss[2] == 0x41) { reVal = System.Text.Encoding.Unicode; } r.Close(); return reVal; } /// 判断是否是不带 BOM 的 UTF8 格式 /// <param name="data"></param> /// <returns></returns> private static bool IsUTF8Bytes(byte[] data) { int charByteCounter = 1; //计算当前正分析的字符应还有的字节数 byte curByte; //当前分析的字节. for (int i = 0; i < data.Length; i++) { curByte = data[i]; if (charByteCounter == 1) { if (curByte >= 0x80) { //判断当前 while (((curByte <<= 1) & 0x80) != 0) { charByteCounter++; } //标记位首位若为非0 则至少以2个1开始 如:110XXXXX...........1111110X if (charByteCounter == 1 || charByteCounter > 6) { return false; } } } else { //若是UTF-8 此时第一位必须为1 if ((curByte & 0xC0) != 0x80) { return false; } charByteCounter--; } } if (charByteCounter > 1) { } return true; } }