Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can OpenFileDialog automatically select the file with the value set in FileName if InitialDirectory is set as well?

This is nit picky but why doesn't the file get automatically selected if it exists and both FileName and InitialDirectory are set correctly?

I have an OpenFileDialog with both FileName and InitialDirectory set correctly and the files exists in this folder. Why isn't the file selected when I run the ShowDialog() method?

No file is selected but it would be nice if it was selected so I wouldn't have to scroll down to select the next file adjacent to it.

Any suggestions?

like image 592
Doug Barense Avatar asked Dec 01 '11 20:12

Doug Barense


People also ask

What does OpenFileDialog use for?

OpenFileDialog component opens the Windows dialog box for browsing and selecting files. To open and read the selected files, you can use the OpenFileDialog. OpenFile method, or create an instance of the System.

What OpenFileDialog does in C#?

The OpenFileDialog component allows users to browse the folders of their computer or any computer on the network and select one or more files to open. The dialog box returns the path and name of the file the user selected in the dialog box.

What is OpenFileDialog box?

The OpenFileDialog control prompts the user to open a file and allows the user to select a file to open. The user can check if the file exists and then open it. The OpenFileDialog control class inherits from the abstract class FileDialog.


1 Answers

Maybe it is not perfect but it meets the expectation somehow.

I have a Button that Shows OpenFileDialog on click event. And async method that will SendKeys to OpenFileDialog.

    private async void button1_Click(object sender, EventArgs e){                 string initialDir = "directory\\";                 string FileName = "filename.smthng";                 string combinedDir = initialDir + FileName;                 if (File.Exists(combinedDir)) // if there is a file with that name at that directory                 {                     openFileDialog1.InitialDirectory = initialDir; // setting directory name                     openFileDialog1.FileName = FileName; // filename                     BeginInvoke((Action)(() => openFileDialog1.ShowDialog())); // we need to use BeginInvoke to continue to the following code.                     await SendKey(FileName); // Sends Key to Dialog                  }                 else // if there is not file with that name works here because no keys need to send.                 {                     openFileDialog1.InitialDirectory = initialDir;                     openFileDialog1.FileName = FileName;                     openFileDialog1.ShowDialog();                 }          }      private async Task SendKey(string FileName){             await Task.Delay(250); // Wait for the Dialog shown at the screen             SendKeys.SendWait("+{TAB}"); // First Shift + Tab moves to Header of DataGridView of OpenFileDialog             SendKeys.SendWait("+{TAB}"); // Second Shift + Tab moves to first item of list             SendKeys.SendWait(FileName); // after sending filename will directly moves it to the file that we are looking for     } 

Result;

openfiledialog

Edit 1;

Okay, For .Net 3.5 there is also TaskParalelLibrary but using Thread will be much easier.

 Thread t;  private const string initialDir = "C:\\";  private const string FileName = "test.txt";  private void button1_Click(object sender, EventArgs e){        string combinedDir = initialDir + FileName;        if (File.Exists(combinedDir)) // if there is a file with that name at that directory             {                 openFileDialog1.InitialDirectory = initialDir; // setting directory name                 openFileDialog1.FileName = FileName; // filename                 BeginInvoke((Action)(() => openFileDialog1.ShowDialog())); // we need to use BeginInvoke to continue to the following code.                 t = new Thread(new ThreadStart(SendKey)); // Sends Key to Dialog with an seperate Thread.                 t.Start(); // Thread starts.             }             else // if there is not file with that name works here because no keys need to send.             {                 openFileDialog1.InitialDirectory = initialDir;                 openFileDialog1.FileName = FileName;                 openFileDialog1.ShowDialog();             }         }                private void SendKey()         {             Thread.Sleep(100); // Wait for the Dialog shown at the screen             SendKeys.SendWait("+{TAB}"); // First Shift + Tab moves to Header of DataGridView of OpenFileDialog             SendKeys.SendWait("+{TAB}"); // Second Shift + Tab moves to first item of list             SendKeys.SendWait(FileName); // after sending filename will directly moves it to the file that we are looking for         } 
like image 54
Berkay Yaylacı Avatar answered Oct 05 '22 17:10

Berkay Yaylacı