本文介绍了如果还设置了InitialDirectory,OpenFileDialog是否可以自动选择具有FileName中设置的值的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这很挑剔,但是如果文件存在且 FileName InitialDirectory 都设置正确,为什么不自动选择文件呢?

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

我有一个 OpenFileDialog ,同时正确设置了 FileName InitialDirectory ,并且文件存在于此文件夹中.为什么在运行 ShowDialog()方法时未选择文件?

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.

有什么建议吗?

推荐答案

也许它并不完美,但以某种方式满足了期望.

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

我有一个 Button ,该按钮在单击事件时显示 OpenFileDialog .还有一个将SendKeys发送到OpenFileDialog的异步方法.

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
    }

结果;

好的,对于 .Net 3.5 ,还有 TaskParalelLibrary ,但是使用Thread会容易得多.

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
        }

这篇关于如果还设置了InitialDirectory,OpenFileDialog是否可以自动选择具有FileName中设置的值的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 09:49