gpt4 book ai didi

c# - 如果还设置了 InitialDirectory,OpenFileDialog 是否可以自动选择具有 FileName 中设置的值的文件?

转载 作者:IT王子 更新时间:2023-10-29 04:22:10 35 4
gpt4 key购买 nike

这有点挑剔,但如果文件存在并且 FileNameInitialDirectory 设置正确,为什么不自动选择该文件?

我有一个 OpenFileDialog,其中 FileNameInitialDirectory 设置正确,文件存在于此文件夹中。为什么当我运行 ShowDialog() 方法时文件没有被选中?

没有文件被选中,但如果它被选中就更好了,这样我就不必向下滚动来选择与其相邻的下一个文件。

有什么建议吗?

最佳答案

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

我有一个 Button,它在点击事件时显示 OpenFileDialog。以及将 SendKeys 发送到 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
}

结果;

openfiledialog

编辑1;

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

 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
}

关于c# - 如果还设置了 InitialDirectory,OpenFileDialog 是否可以自动选择具有 FileName 中设置的值的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8347585/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com