Parse FileDialog FilterString

S-Soft 646 Reputation points
2023-03-07T10:06:01.5233333+00:00

Hello,

I have a FileDialog.FilterString like this:

"All Files (.)|.ex1;.ext2|blah1 (.ex1)|.ex1|blah2 (.ext2)|.ext2"

And need to parse out the supported extensions from it, in this case:

.*
.ex1

.ext2

Asked here so maybe someone can suggest a safe short way to do so, preferred without regex :)

Thanks all

  • OR: If OFD.Filter.Contains(Path.GetExtension(OFD.FileName).ToLowerInvariant) = False/True Then... Is this reliable to check if extensions inside FilterString are selected? Except *. *
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,605 questions
{count} votes

2 answers

Sort by: Most helpful
  1. LesHay 7,126 Reputation points
    2023-03-07T13:42:22.56+00:00

    Hi

    Not sure what you are asking for here. You have the user select a file from a dialog which has that filter set for it - then the filename returned will have the extension and in turn, it is simple to get the extension from that filename.

    Where, in that process do you want to parse the filter? What reason to bother to parse it (presumably you set up the filter in your code in the first place)?

    The filter, being a simple string can be manipulated quite easily as would any sting.

    1 person found this answer helpful.

  2. Jiachen Li-MSFT 27,166 Reputation points Microsoft Vendor
    2023-03-13T09:43:15.9033333+00:00

    Hi @S-Soft ,

    Please check if the following code helps. If I misunderstand your question, please point it out.

    Select Case Path.GetExtension(myDialog.FileName).ToLower()
        Case ".
            ' ...
    End Select
    

    Best Regards.
    Jiachen Li
    ----------
    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments