none
Check if the file is not a expected format RRS feed

  • Question

  • Hello,

    I need help regarding initial file check.

    I have a browse button and a textbox to browse .CSV file.

    I need to give a Messagebox if i select file of different format like "Invalid file" or something like that.

    How do i do it.

    Thanks in advance


    Akshay

    Tuesday, March 19, 2019 11:32 AM

Answers

  • Hi

    Thank you for posting here.

    Based on your description, you want to give a message box if you select other files except for the CSV file.

    You could try the following code.

      private void button1_Click(object sender, EventArgs e)
            {
                using (OpenFileDialog openFileDialog = new OpenFileDialog())
                {
                    if (openFileDialog.ShowDialog() == DialogResult.OK)
                    {
                        string m = openFileDialog.FileName;
                        if (m.Contains("csv"))
                        {
                            textBox1.Text=m;
                        }
                        else
                        {
                            MessageBox.Show("Your chooose is incorrect,Invalid file");
                        } 
                    }
                }
            }
    

    Result:

    Hope my solution could be helpful.

    Best regards,

    Jack



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by anonymous_3210 Wednesday, March 20, 2019 5:09 AM
    Wednesday, March 20, 2019 2:54 AM
    Moderator

All replies

  • Depends on how you want to check. If you just want to make sure the user selects a .csv file then in your OpenFileDialog ensure the file filter only allows .csv files. When you retrieve the filename from the dialog then verify it ends with a .csv using Path.GetExtension.

    Note however that file extensions are just recommendations and nothing is actually enforced so if you need to "verify" the file is truly a particular format then you need to read the file. Most binary file formats have a file header that allow you to read the first handful of bytes to determine whether the file is "valid". Text based formats like CSV do not though. You could use heuristics to try to figure it out but there could always be something wrong with file even if it was in the correct format so most code simply tries to read the file assuming it is correct and then handling any errors that may occur while reading.


    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, March 19, 2019 1:49 PM
    Moderator
  • Hi

    Thank you for posting here.

    Based on your description, you want to give a message box if you select other files except for the CSV file.

    You could try the following code.

      private void button1_Click(object sender, EventArgs e)
            {
                using (OpenFileDialog openFileDialog = new OpenFileDialog())
                {
                    if (openFileDialog.ShowDialog() == DialogResult.OK)
                    {
                        string m = openFileDialog.FileName;
                        if (m.Contains("csv"))
                        {
                            textBox1.Text=m;
                        }
                        else
                        {
                            MessageBox.Show("Your chooose is incorrect,Invalid file");
                        } 
                    }
                }
            }
    

    Result:

    Hope my solution could be helpful.

    Best regards,

    Jack



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by anonymous_3210 Wednesday, March 20, 2019 5:09 AM
    Wednesday, March 20, 2019 2:54 AM
    Moderator
  • That's perfect.

    Thank you so much


    Akshay

    Wednesday, March 20, 2019 5:10 AM