开发者

How to match a file path with a file search pattern (i.e. *.jpg)?

I know how to get all files that match the search pattern in a folder like this:

DirectoryInfo folderInfo = new DirectoryInfo(folderPath);

FileInfo[] fileInfos = folderInfo.GetFiles(searchPattern);

But my problem is, if I already know the file path, how can I match it with the search pattern? For compatibility, the search pattern has to be the same format like *.jpg or something开发者_JS百科.


I'm interpreting your question as meaning you already have a string for the file path and just want to check whether it matches a certain pattern. For this you first need to consider whether pattern patching is really what you need, it may be preferable to just use the extension directly for example.

Assuming that is not an option, you're probably going to want to use regular expressions. You will need to convert the string with wild-cards to a regular expression. Unfortunately I'm not aware of any built in ways of doing this, but it should be possible to do by simply escaping any characters which would have meaning in a regex and replacing the wild cards with appropriate regular expressions.


Well if you already have the full file path and you isolate the extension only, using Path.GetExtension or similar, then you just add a * before you are are set, right?


If u already know the path, Don't use search pattern. Try

FileInfo fi = new FileInfo(FilePath);


you can use Directory.GetFiles like "Directory.GetFiles("d:/xxx/*.jpg")" to get string array of all match files, and then to open or handle the single file.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜