c#无法在有其他文件的目录中创建文件

本文关键字:文件 创建 其他 | 更新日期: 2023-09-27 18:03:30

谁能帮帮我,我刚学习c#大约2个月,我有这个问题,我正在构建一个从临时文件过滤数据的类,并在目录内的新TXT文件中创建结果,如果目录是空的,也不在同一日期,它会完美地构建,如果在同一日期有另一个文件,它应该创建增加lastname的最后数字。

我的问题是,当我运行代码时,如果目录中有相同日期的文件,则不会创建,那么结果应该是这样的:

  • C: ' result_2014051301.txt
  • C: ' result_2014051401.txt
  • C:'result_2014051402.txt <—Failed, it is not .2014051401.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);
                    }
                  }
               }
           }
        }
     }
    

c#无法在有其他文件的目录中创建文件

这部分功能不正常:

Int32.Parse(lastFilename.Substring(0, lastFilename.Length - 4).Replace(pathX + filename, ""));

pathX + filenameC:'folderfile.txt不是C:'folder'file.txt

您需要添加'或调用Path.Join

这将导致Parse操作失败,因为它试图使用who字符串(减去扩展名)。