Search code examples
c#wpfopenfiledialog

How to filter a custom format with the OpenFileDialog in c# WPF


I'm trying to add a filter to my OpenFileDialog with a custom file format that I created This is what I have so far:

private void btnSymKey_Click(object sender, RoutedEventArgs e)
    {
        OpenFileDialog openFileDialog1 = new OpenFileDialog();
        openFileDialog1.Filter = "public key (*.publ)";
        openFileDialog1.FilterIndex = 2;
        if (openFileDialog1.ShowDialog() == true)
        {
            DecryptionPathes.encryptedKey = System.IO.Path.GetFullPath(openFileDialog1.FileName);

            txtSymKeyPath.Text = DecryptionPathes.encryptedKey;

            this.btnSafeDecrypt.Visibility = Visibility.Visible;
            this.btnSafeDecrypt.IsEnabled = true;
            this.txtSafeDecryptPath.Visibility = Visibility.Visible;
        }
    }

But this doesnt work since the OpenFileDialog doesn't know ".publ" is there still a way to filter these files?


Solution

  • Try below code

    openFileDialog1.Filter = "public key  (*.publ)|*.publ";