I'm using this powershell code to check if a file starting with 4 specific chars exist in a specific path or not
$myPath = "c:\myFolder\";
$myFileBegin = "abc_";
test-path "$myPath$myFileBegin*";
It works and it's simple, but now I'm trying to do the same thing with C#.
I don't report all the code but basically I'm getting all files from the folder with Directory.GetFiles
and looping over those files and testing with Regex.IsMatch
.
Is this the correct way or is there a faster one like in powershell?
You can specify a search filter in the call to Directory.GetFiles()
:
Directory.GetFiles(@"c:\myFolder\", "abc_*.*");
This will return only files that start with abc_
.
尝试这个
Directory.GetFiles(@"c:\myFolder\").Where(x => x.StartsWith("abc_"));
with .net 4 and above:
Directory.EnumerateFiles(@"c:\myFolder", "abc_*.*")).Any() ? true : false
this return (bool)
true or false belong the case
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.