Filedialog doubt c#

Asked

Viewed 40 times

2

Good, 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 button2 and button3 = Enabled. What I want now is that if you choose a file . cnf button2 gets Enabled = true; and button3 Enabled = false;

And if you choose the . csr file the other way around. Can you help? Thank you.

2 answers

3

Usa Path.GetExtension

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

2


You can do it like this:

 button2.Enabled = Path.GetExtension(openFileDialog1.FileName) == ".cnf";
 button3.Enabled = Path.GetExtension(openFileDialog1.FileName) == ".csr";

Browser other questions tagged

You are not signed in. Login or sign up in order to post.