【问题标题】:C# fail to create file in directory if has other file如果有其他文件,C# 无法在目录中创建文件
【发布时间】: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);
                    }
    
                  }
               }
           }
        }
     }
    

【问题讨论】:

  • 确保包含应用程序失败时发生的任何异常,它们对于找到问题的根源非常有帮助。

标签: c# parsing getfiles


【解决方案1】:

此部分无法正常运行:

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

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

您需要添加\ 或致电Path.Join

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

【讨论】:

  • @user3641945:你需要什么帮助?
  • @user3641945:我不会免费为互联网上的陌生人修复代码。我会帮助你学习,但不会解决你的问题。见鬼,上次我解决别人的问题时,他们删除了我的帖子。
  • @Guvante 好消息是只有版主可以删除已有答案的主题!
  • @tnw:很高兴听到,很久很久以前 :)
  • @user3641945 对答案表示感谢的好方法是接受它。 :)
猜你喜欢
  • 2015-05-28
  • 2020-10-18
  • 2014-09-21
  • 1970-01-01
  • 2014-10-06
  • 2016-03-03
  • 2013-10-05
  • 2015-03-30
  • 2011-02-16
相关资源
最近更新 更多