【发布时间】:2014-05-15 18:12:35
【问题描述】:
谁能帮帮我,我刚学C#大约2个月,我有这个问题, 我正在构建一个用于从临时文件中过滤数据的类,并在目录内的新 txt 文件中创建结果,如果目录为空也不在同一日期,它会完美构建,如果在同一日期有另一个文件,它应该随着增加而创建lastname 的最后一个数字。
我在运行代码时遇到的问题是,如果目录中有相同日期的文件,则不会创建,那么结果应该是这样的:
- C:\result_2014051301.txt
- C:\result_2014051401.txt
-
C:\result_2014051402.txt
class Entity2 { public Entity2() { string fileTemp = "DEFAULT.temp"; string indexD = Properties.Settings.Default.ChIndex2D; string indexC = Properties.Settings.Default.ChIndex2C; string indexS = Properties.Settings.Default.ChIndex2S; string tempPath = AppDomain.CurrentDomain.BaseDirectory; string targetPath = Properties.Settings.Default.ExtractALL_DIR; string SourceFile = Path.Combine(tempPath, fileTemp); string tempFileX = Path.GetTempFileName(); if (!System.IO.Directory.Exists(targetPath)) { System.Windows.Forms.MessageBox.Show("Error missing .temp", "Message Box"); } else { string ext = ".txt"; int sequence = 0; DateTime dateFileName = DateTime.Today; string discode = Properties.Settings.Default.ChannelID_2; string filename = discode + "_" + dateFileName.ToString("yyyyMMdd"); string pathX = Properties.Settings.Default.ExtractALL_DIR + @"/Channel2"; if (!Directory.Exists(pathX)) { Directory.CreateDirectory(pathX); } string[] files = Directory.GetFiles(pathX, filename + "*.txt", SearchOption.TopDirectoryOnly); if (files.Length > 0) { Array.Sort(files); string lastFilename = files[files.Length - 1]; sequence = Int32.Parse(lastFilename.Substring(0, lastFilename.Length - 4).Replace(pathX + filename, "")); } sequence++; string newFileName = filename + sequence.ToString().PadLeft(2, '0') + ext; string DestFile = Path.Combine(pathX, newFileName); using (var ab = new StreamReader(SourceFile)) using (var cd = new StreamWriter(DestFile)) { string lineX; while ((lineX = ab.ReadLine()) != null) { if (lineX.LastIndexOf("100", 3) != -1 || lineX.LastIndexOf("MGR", 15) != -1 || lineX.LastIndexOf(indexC, 15) != -1) { lineX = lineX.Replace(indexD, ""); lineX = lineX.Replace("DEFAULT", discode); if (lineX.LastIndexOf("800", 3) != -1) { lineX = lineX.Replace(indexS, ""); } cd.WriteLine(lineX); } } } } } }
【问题讨论】:
-
确保包含应用程序失败时发生的任何异常,它们对于找到问题的根源非常有帮助。