如何使用c#获取特定文件名
我的文件夹中有 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果您只对包含破折号的 zip 文件感兴趣,则可以向 Directory.GetFiles 提供搜索模式。
有关这些搜索模式的更多信息,请查看此链接: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.
Check out this link for more information on those search patterns: http://msdn.microsoft.com/en-us/library/wz42302f.aspx
我想
如果
-
始终出现在您感兴趣的文件名中,或者
如果您感兴趣的文件名始终以
abc-
开头,您就可以这样做。I guess you can do
if the
-
is always present in the filenames you are interested inor
if the filenames always start with
abc-
for the ones you are interested in.你可以做
if(filename.StartsWith ("abc-") )
或者你可以做if (filename.Contains ( "-" ) )
或者你可以做>string [] fileEntries = Directory.GetFiles(targetDirectory, "abc-*.Zip");
you can do
if(filename.StartsWith ("abc-") )
or you can doif (filename.Contains ( "-" ) )
or you can dostring [] fileEntries = Directory.GetFiles(targetDirectory, "abc-*.Zip");
或者,您可以使用正则表达式,如下所示:
Alternatively, you can use a regular expression as follows:
您可以使用与您的文件名匹配的正则表达式,大致如下:
正则表达式“abc-\d+.zip”意味着
字符串“abc-”后跟任意数量的数字,然后是 .后跟字符串“zip”(正则表达式语法 )
You could use a regular expression that matches your filenames, something along thees lines:
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)