private bool GetSNcsv() { LogFileName = ResultDir + "\\" + DateTime.Now.ToString("yyyyMMdd_HHmmss") + ".csv"; DataTable dt = new DataTable(); StringBuilder sb = new StringBuilder(); bool isFirstCsv = true; if (TargetFileName == null) { TargetFileError = "Please select the target files!"; return(false); } foreach (string fileName in TargetFileName.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries)) { if (!File.Exists(fileName)) { TargetFileError = "The target files does not exist, please choose again!"; return(false); } FileInfo fileNameInfo = new FileInfo(fileName); if (fileNameInfo.Extension != ".csv") { TargetFileError = "The target files format is not correct, please select .CSV file!"; return(false); } //标示是否读取到所需的行 bool isDataLine = false; using (StreamReader sr = new StreamReader(fileName, Encoding.UTF8)) { //记录每次读取的一行记录 string strLine = string.Empty; //记录每行记录中的各字段内容 string[] aryLine = null; string[] tableHead = null; //标示列数 int columnCount = 0; //逐行读取CSV中的数据 //if (!sr.ReadToEnd().ToUpper().Contains("SERIALNUMBER")) //{ // TargetFileError = "目标文件格式不正确!"; // return false; //} while ((strLine = sr.ReadLine()) != null) { if (strLine.ToUpper().Contains("SERIALNUMBER") && isFirstCsv) { sb.AppendLine(strLine); tableHead = strLine.Split(','); //创建列 for (int i = 0; i < tableHead.Length; i++) { tableHead[i] = tableHead[i].Replace("\"", ""); DataColumn dc = new DataColumn(tableHead[i]); dt.Columns.Add(dc); } } else { if (isDataLine == true) { aryLine = strLine.Split(','); columnCount = aryLine.Length; int differences = columnCount - dt.Columns.Count; if (differences > 0) { for (int i = 0; i < differences; i++) { DataColumn dc = new DataColumn(); dt.Columns.Add(dc); } dt.Rows.Add(aryLine); continue; } DataRow dr = dt.NewRow(); for (int j = 0; j < columnCount; j++) { dr[j] = aryLine[j].Replace("\"", ""); } dt.Rows.Add(dr); } else if (isFirstCsv) { sb.AppendLine(strLine); } if (strLine.ToUpper().Contains("LOWER LIMIT----->")) { isFirstCsv = false; isDataLine = true; } } } } } if (isFirstCsv) { TargetFileError = "The CSV file format is not correct, please choose again!"; return(false); } //DataTable dt1 = dt.Clone(); if (SNTextBox == null || SNTextBox == string.Empty) { if (SNFileName == null) { SNError = "Please select a SN files or double-click to input SN!"; return(false); } if (!File.Exists(SNFileName)) { SNError = "SN file does not exist, please choose again!"; return(false); } using (StreamReader sr = new StreamReader(SNFileName)) { string content = sr.ReadToEnd(); string[] sns = Regex.Split(content, "\r\n", RegexOptions.IgnoreCase); //sn = sr.ReadToEnd().Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); string rowFilter = string.Empty; int a = 0; foreach (string sn in sns) { a++; rowFilter += "SerialNumber='" + sn + "'"; if (sns.Length != a) { rowFilter += " or "; } } //rowFilter = "SerialNumber='Z73R7Q01HP193ZY9'"; //DataRow[] getRows = dt.Select(rowFilter); //foreach (DataRow row in getRows) //{ // dt1.Rows.Add(row.ItemArray); //} dt.DefaultView.RowFilter = rowFilter; //dt.DefaultView.Sort = ""; } } else { string[] sns = Regex.Split(SNTextBox, "\r\n", RegexOptions.IgnoreCase); //sn = sr.ReadToEnd().Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); string rowFilter = string.Empty; int a = 0; foreach (string sn in sns) { a++; rowFilter += "SerialNumber='" + sn + "'"; if (sns.Length != a) { rowFilter += " or "; } } dt.DefaultView.RowFilter = rowFilter; } DataTable dtView = dt.DefaultView.ToTable(); ResultStr = string.Format("A total of {0} records", dtView.Rows.Count); if (dtView.Rows.Count == 0) { return(false); } for (int i = 0; i < dtView.Rows.Count; i++) //写入各行数据 { string data = string.Empty; for (int j = 0; j < dtView.Columns.Count; j++) { string str = dtView.Rows[i][j].ToString(); str = str.Replace("\"", "\"\""); //替换英文冒号 英文冒号需要换成两个冒号 if (str.Contains(',') || str.Contains('"') || str.Contains('\r') || str.Contains('\n')) //含逗号 冒号 换行符的需要放到引号中 { str = string.Format("\"{0}\"", str); } data += str; if (j < dt.Columns.Count - 1) { data += ","; } } sb.AppendLine(data); } using (StreamWriter sw = new StreamWriter(LogFileName)) { sw.WriteLine(sb.ToString()); } return(true); }
public override void Execute() { if (string.IsNullOrEmpty(Content)) { return; } DTE dte = base.GetService <DTE>(true); string fileWithContent = ""; string testcontent = Content.Trim(new char[] { ' ', '\t', '\n', '\r' }); if ((testcontent == "") || (testcontent == "dummy")) { //no content, check for sourcefile if (SourceFileName == null) { return; } if (SourceFileName == "") { return; } fileWithContent = this.SourceFileName; string templateBasePath = GetTemplateBasePath(); if (!Path.IsPathRooted(fileWithContent)) { fileWithContent = Path.Combine(templateBasePath, fileWithContent); fileWithContent = new FileInfo(fileWithContent).FullName; } } else { fileWithContent = Path.GetTempFileName(); using (StreamWriter writer = new StreamWriter(fileWithContent, false)) { writer.Write(content); writer.Close(); } } if ((TargetFileName == null) || (TargetFileName == "")) { if (File.Exists(fileWithContent)) { TargetFileName = Path.GetFileName(fileWithContent); } } Project projectByName = Helpers.GetProjectByName(dte, projectName); if (projectByName != null) { ProjectItems whereToAdd = projectByName.ProjectItems; //check if item exists ProjectItem existingFile = null; try { existingFile = Helpers.GetProjectItemByName(whereToAdd, TargetFileName); } catch (Exception) { } if (existingFile != null && !Overwrite) { Helpers.LogMessage(dte, this, "File " + TargetFileName + " exists and will not be overwritten"); return; } //if targetfilename ends with .tt, we remove any file with the same start if (TargetFileName.EndsWith(".tt")) { //is there any file with the same name, but different extension foreach (ProjectItem pitem in whereToAdd) { if (Path.GetFileNameWithoutExtension(pitem.Name.ToUpper()) == Path.GetFileNameWithoutExtension(TargetFileName.ToUpper())) { string deletedFilepath = Helpers.GetFullPathOfProjectItem(pitem); //pHelpers.GetFullPathOfProjectItem(item); //delete the file pitem.Remove(); File.Delete(deletedFilepath); break; } } } ProjectItem _ProjectItem = Helpers.AddFromTemplate(whereToAdd, fileWithContent, this.TargetFileName); } else { //do nothing if project is not found //throw new Exception("Project with name " + projectName + " not found in solution"); } }