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;
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
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…