winforms 我在Visual Studio“Windows Forms App(.NET Framework)”中使用OpenFileDialog打开“选择文件夹”,但提示符没有打开,我该如何解决这个问题?

ozxc1zmp  于 2023-06-24  发布在  Windows
关注(0)|答案(1)|浏览(180)
private void locbtn_Click(object sender, EventArgs e)
{
    OpenFileDialog openFileDialog1 = new OpenFileDialog();
    openFileDialog1.Title = "Select Folder";
    openFileDialog1.InitialDirectory = @"C:\";//--"C:\\";
    openFileDialog1.ValidateNames = false;
    openFileDialog1.CheckFileExists = false;
    openFileDialog1.CheckPathExists = true;
    openFileDialog1.ShowDialog();

    if (openFileDialog1.FileName != "")
    {
        textBox1.Text = openFileDialog1.FileName;
    }

    if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    {
    }
    else
    {
        textBox1.Text = "Please Select an Another Folder!";
    }
}

这是密码。当我单击locbtn时它应该显示,但它似乎拒绝了。
这段代码审核了2天,能给予我一个答复吗?我尝试了多个网站的(+ Youtube)源代码,和...什么都没发生我也试过FolderBrowserDialog,但没有任何变化。

mf98qq94

mf98qq941#

我将重新排列代码,以便您可以确定发生了什么。您的代码有两个openFileDialog.ShowDialog()语句。很可能错过了返回ShowDialog()
尝试类似于:

private void locbtn_Click(object sender, EventArgs e)
{
    string fileTextDefault = "Please Select Another Folder!";
    TestBox1.Text = fileTextDefault;

    OpenFileDialog openFileDialog1 = new OpenFileDialog();
    openFileDialog1.Title = "Select Folder";
    openFileDialog1.InitialDirectory = @"C:\";//--"C:\\";
    openFileDialog1.ValidateNames = false;
    openFileDialog1.CheckFileExists = false;
    openFileDialog1.CheckPathExists = true;
    openFileDialog.Filter = "All Files (*.*)|*.*";
    
    if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    {
        var filePath = openFileDialog1.FileName;
        if (filePath != "")
        {
            textBox1.Text = filePath;
        }
    }
    else
    {
      //Do something here to indicate failure
    }
}

相关问题