如何使用c#获取特定文件名

发布于 2024-12-09 07:54:42 字数 769 浏览 2 评论 0原文

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

目标目录 = "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 这些文件名。

有人对此提出任何想法吗?

非常感谢...

I have 10 zip files in a folder having path like this

TargetDirectory = "C:\docs\folder\"

some of the zip files names are like this

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

and some are like this...

doc.zip
doc123.zip  
..

I am getting all file names by using following code...

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
   }
}

What i have to put in the double quotes in this line if(filename == "") to get like abc-19870908.Zip these filenames.

Would any one please suggest any idea about this?

Many thanks...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

请别遗忘我 2024-12-16 07:54:42

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

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

有关这些搜索模式的更多信息,请查看此链接:http://msdn.microsoft.com/en-我们/library/wz42302f.aspx

If you're only interested in zip files containing a dash, you can provide a search pattern to Directory.GetFiles.

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

Check out this link for more information on those search patterns: http://msdn.microsoft.com/en-us/library/wz42302f.aspx

陌路黄昏 2024-12-16 07:54:42

我想

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

如果 - 始终出现在您感兴趣的文件名中

,或者

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

如果您感兴趣的文件名始终以 abc- 开头,您就可以这样做。

I guess you can do

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

if the - is always present in the filenames you are interested in

or

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

if the filenames always start with abc- for the ones you are interested in.

拥有 2024-12-16 07:54:42

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

you can do if(filename.StartsWith ("abc-") ) or you can do if (filename.Contains ( "-" ) ) or you can do string [] fileEntries = Directory.GetFiles(targetDirectory, "abc-*.Zip");

却一份温柔 2024-12-16 07:54:42
// 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
   }
 }
// Consider using this overload:
// public static string[] GetFiles( string path, string searchPattern)

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

Alternatively, you can use a regular expression as follows:

string [] fileEntries = Directory.GetFiles(targetDirectory);
foreach(string fileName in fileEntries)
{
   if(Regex.Match (filename, @"abc.*?\.zip", RegexOptions.IgnoreCase))
   {
      // i want to do something
   }
 }
无可置疑 2024-12-16 07:54:42
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
}
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
}
半夏半凉 2024-12-16 07:54:42

您可以使用与您的文件名匹配的正则表达式,大致如下:

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”(正则表达式语法

You could use a regular expression that matches your filenames, something along thees lines:

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
  }

}

The regular expression "abc-\d+.zip" means
the string "abc-" followed by any number of digits, followed by a . followed by the string "zip" (regular expression syntax)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文