【问题标题】:how to get specific file names using c#如何使用c#获取特定文件名
【发布时间】:2011-10-11 20:23:09
【问题描述】:

我在一个文件夹中有 10 个 zip 文件,其路径如下

TargetDirectory = "C:\docs\folder\"

一些zip文件名是这样的

abc-19870908.Zip 
abc-19870908.zip
abc-12345678.zip

有些是这样的……

doc.zip
doc123.zip  
..

我正在使用以下代码获取所有文件名...

string [] fileEntries = Directory.GetFiles(targetDirectory); 
foreach(string fileName in fileEntries)
{
  // here I need to compare , 
  // I mean I want to get only these files which are having 
  // these type of  filenames `abc-19870908.Zip`

  if (filename == "")
  {

       // I want to do something
   }
}

我必须在这行 if(filename == "") 的双引号中加上 abc-19870908.Zip 这些文件名。

有人可以就此提出任何想法吗?

非常感谢...

【问题讨论】:

    标签: c# .net winforms file filenames


    【解决方案1】:

    如果您只对包含破折号的 zip 文件感兴趣,您可以向 Directory.GetFiles 提供搜索模式。

    string [] fileEntries = Directory.GetFiles(targetDirectory, "*-*.zip"); 
    

    查看此链接以获取有关这些搜索模式的更多信息:http://msdn.microsoft.com/en-us/library/wz42302f.aspx

    【讨论】:

      【解决方案2】:

      我想你可以做到

       if (filename.Contains("-"))
       {
          ...
       }
      

      如果- 总是出现在您感兴趣的文件名中

       if (filename.StartsWith("abc-"))
       {
          ...
       }
      

      如果您感兴趣的文件名总是以abc- 开头。

      【讨论】:

        【解决方案3】:

        你可以if(filename.StartsWith ("abc-") )或者if (filename.Contains ( "-" ) )或者string [] fileEntries = Directory.GetFiles(targetDirectory, "abc-*.Zip");

        【讨论】:

          【解决方案4】:
          // Consider using this overload:
          // public static string[] GetFiles( string path, string searchPattern)
          
          string [] fileEntries = Directory.GetFiles(targetDirectory, "abc*.zip");
          

          或者,您可以使用正则表达式,如下所示:

          string [] fileEntries = Directory.GetFiles(targetDirectory);
          foreach(string fileName in fileEntries)
          {
             if(Regex.Match (filename, @"abc.*?\.zip", RegexOptions.IgnoreCase))
             {
                // i want to do something
             }
           }
          

          【讨论】:

            【解决方案5】:
            List<String> files = Directory.GetFiles(@"C:\docs\folder\").ToList();
            var g = from String s in files where s.StartsWith("abc") select s;
            foreach(var z in g)
            {
               //Do stuff in here as a replacement for your if
            }
            

            【讨论】:

              【解决方案6】:

              您可以使用与您的文件名匹配的正则表达式,类似于这些行:

              string sPattern = "abc-\d+\.zip";
              
              string [] fileEntries = Directory.GetFiles(targetDirectory); 
              foreach(string fileName in fileEntries)
              {
                // here i need to compare , i mean i want to get only these files which are having these type of  filenames `abc-19870908.Zip`
                if(System.Text.RegularExpressions.Regex.IsMatch(filename , sPattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase))
                {
              
                     // i want to do something
                }
              
              }
              

              正则表达式“abc-\d+.zip”表示 字符串 "abc-" 后跟任意位数,然后是 .后跟字符串 "zip" (regular expression syntax)

              【讨论】:

                猜你喜欢
                • 2023-03-02
                • 2017-09-08
                • 1970-01-01
                • 1970-01-01
                • 2018-04-03
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多