-1

I'm trying to handle a file explorer in C# but I don't know how to select the "All Files" option enter image description here

I've been successful in typing a file path into the file name text box and I'm able to press the OK button. But I can't select the file if I can't filter by "All Files"

System.Windows.Forms.SendKeys.SendWait(@"C:\Users\email\Desktop\Hardware Hub\images\" + ID + ".png");
System.Windows.Forms.SendKeys.SendWait("{ENTER}");
stuartd
  • 70,509
  • 14
  • 132
  • 163
zaid iqbal
  • 101
  • 1
  • 2
  • 11
  • Can you show us the code you used to generate "Custom Files" and "All Files" ? What happens when you click "All Files" ? – TheLeb Sep 08 '20 at 15:57

1 Answers1

0

You don't have to use SendKeys. You use the FileName property to populate the input field and use the FilterIndex property to select "All Files" on your OpenFileDialog instance.

var fileContent = string.Empty;
var filePath = string.Empty;

using (OpenFileDialog openFileDialog = new OpenFileDialog())
{
    openFileDialog.InitialDirectory = "c:\\";
    openFileDialog.FileName = "Your filename"; // Sets the file name input
    openFileDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
    openFileDialog.FilterIndex = 2; // Sets filter to 'All Files'
    openFileDialog.RestoreDirectory = true;

    if (openFileDialog.ShowDialog() == DialogResult.OK)
    {
        //Get the path of specified file
        filePath = openFileDialog.FileName;

        //Read the contents of the file into a stream
        var fileStream = openFileDialog.OpenFile();

        using (StreamReader reader = new StreamReader(fileStream))
        {
            fileContent = reader.ReadToEnd();
        }
    }
}

MessageBox.Show(fileContent, "File Content at path: " + filePath, MessageBoxButtons.OK);
Philipp H.
  • 552
  • 1
  • 3
  • 19