FileDialog doubt c #

2

Well, I have the following code in my program:

CODE

 private void button1_Click(object sender, EventArgs e)
    {
        openFileDialog1.Filter = "Ficheiro de Configuração (*.cnf)|*.cnf|Ficheiro de Request (*.csr)|*.csr";
        DialogResult resposta = openFileDialog1.ShowDialog();
        if (resposta == DialogResult.OK)
        {

            string arquivo = openFileDialog1.FileName;

            VariaveisGlobais.CNF = arquivo;
            VariaveisGlobais.CSR = label2.Text;
        }

        label2.Text = Path.GetFileNameWithoutExtension(openFileDialog1.FileName);
        button2.Enabled = true;
        button3.Enabled = true;
        textBox3.Text = openFileDialog1.FileName;
    }

As you can see, when FileDialog receives something, it puts the button2 and button3 = Enabled. What I want now is that if the user chooses a .cnf file the button2 becomes Enabled = true; and button3 Enabled = false;

And if you choose the .csr file the reverse. Can you help? Thank you.

    
asked by anonymous 19.05.2015 / 16:35

2 answers

2

You can do this:

 button2.Enabled = Path.GetExtension(openFileDialog1.FileName) == ".cnf";
 button3.Enabled = Path.GetExtension(openFileDialog1.FileName) == ".csr";
    
19.05.2015 / 16:54
3

Use Path.GetExtension

switch(Path.GetExtension(openFileDialog1.FileName))
{
    case ".cnf":
        //...
        break;
    case ".csr":
        //...
        break;
    default:
        throw new InvalidOperationException("Formato nao suportado");
}
    
19.05.2015 / 16:49