OpenFileDialog file extension filtering

Koray Genç 21 Reputation points
2022-11-17T23:20:54.897+00:00

in filtering = all files selected
I don't want .exe or .pdf file to be uploaded, is it possible?

Developer technologies | Windows Forms
{count} votes

Answer accepted by question author
  1. Anonymous
    2022-11-18T02:54:12.493+00:00

    Hi @Koray Genç , Welcome to Microsoft Q&A, you could try the following code to get what you wanted.

    Because DLG. Filter is to filter out the displayed files. So an easy way to do this is to put the suffix you need to display.

    OpenFileDialog dlg = new OpenFileDialog();  
      
    // Filter by Word Documents OR Excel Worksheets OR PowerPoint Presentations   
    //           OR Office Files   
    //           OR All Files  
    //dlg.Filter = "Word Documents|*.doc|Excel Worksheets|*.xls|PowerPoint Presentations|*.ppt" +  
    //  "|Office Files|*.doc;*.xls;*.ppt" +  
    //  "|All Files|*.*";  
      
    dlg.Filter = "All Files|*.doc;*.xls;*.ppt;*.doc;.xls;*.ppt;*.txt;";  
      
    dlg.ShowDialog();  
    

    Update:

    OpenFileDialog dlg = new OpenFileDialog();  
    dlg.Filter = "All Files|*.*";  
    bool Flag = true;  
    while (Flag)  
    {  
    	if (dlg.ShowDialog() == DialogResult.OK)  
    	{  
    		if (dlg.SafeFileName.Contains(".exe") | dlg.SafeFileName.Contains(".pdf"))  
    		{  
    			MessageBox.Show("This File Cannot Be Select");  
    			dlg.FileName = "";  
    		}  
    		else  
    		{  
    			Flag = false;  
    		}  
    	}  
    	else  
    	{  
    		MessageBox.Show("Select error");  
    	}  
    }  
    

    Best Regards,
    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    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.


1 additional answer

Sort by: Most helpful
  1. Koray Genç 21 Reputation points
    2022-11-18T04:05:19.927+00:00
            Dim OFP As New OpenFileDialog  
            Dim fileType As String = ".exe"  
            OFP.FileName = ""  
            OFP.Multiselect = False  
            OFP.Filter = "All Files|*.*||" & fileType  
      
             If (OFP.ShowDialog() = DialogResult.OK) Then  
      
      
      
                Dim ext As String = Path.GetExtension(OFP.FileName)  
                TextBox1.Text = OFP.FileName  
      
                If ext <> fileType Then  
      
                Else  
                    MessageBox.Show("This File Cannot Be Selected")  
                    TextBox1.Text = ""  
      
                End If  
            End If  
      
    

    I tried a code like this :)

    and the result is nice

    thank you for your help


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.