lundi 21 juin 2021

If statement to filter file extensions (C#)

I want to proceed in creating a program that does different events with different file extensions, with files opened with . I understand things like Path.GetExtension, and these answers: Refactoring if-else statements checking for different file extensions C# custom file extension filter

Can someone explain me a straight forward how to create a button "If Statement" meant at executing say, if a ".jpeg" file is detected as the selected file's extension, while "else" may bring up message box, like "Improper File type uploaded."

Here is cut of the code where action takes place:

private void BtnMediaPlayer_Click(object sender, EventArgs e)
{
    // Open File Dialog
    OpenFileDialog open = new OpenFileDialog();
    // Image Filters
    open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";
    if (open.ShowDialog() == DialogResult.OK)
    {
        // display image in picture box  
        pictureBox1.Image = new Bitmap(open.FileName);


    }



}

As can be seen, the program opens an Open File Dialog that then is supposed to filter for certain files, and then Open them within the pciture box, but how do I rewrite it so that, "if (open.ShowDialog() == DialogResult.OK)" or the surrounding code produces different results (executions based on the If Statements) based on the file type(s)?

TL DR How to write If Statements for Different files uploaded?

Aucun commentaire:

Enregistrer un commentaire