首先,我使用 Visual Studio 2013 和 C# 编码来开发 Windows 窗体应用程序。我添加了“System.IO”命名空间。
我需要在用户选择时在文本框中显示目录路径。 该代码在用户从弹出窗口中选择文件夹的位置正常工作 然后按 OK 按钮,然后显示其中的文件数 该文件夹 - 但文件夹路径未按我的要求显示。
代码如下所示:
private void button1_Click(object sender, EventArgs e)
{
//
// This event handler was created by clicking the button in the application GUI.
//
DialogResult button1_Click = folderBrowserDialog1.ShowDialog();
if (button1_Click == DialogResult.OK)
{
//
// The user selected a folder and pressed the OK button.
// A message pops up and identifies the number of files found within that folder.
//
string[] files = Directory.GetFiles(folderBrowserDialog1.SelectedPath);
MessageBox.Show("Files found: " + files.Length.ToString(), "Message");
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
string path;
path = folderBrowserDialog1.SelectedPath;
// folderBrowserDialog1.ShowDialog(); // NOT SURE ABOUT USING THIS!
textBox1.Text = path;
}
最佳答案
您可以将其添加到 button1_Click
方法的末尾(在 if
block 内):
textBox1.Text = folderBrowserDialog1.SelectedPath;
关于c# - 用户选择时如何在文本框中显示目录路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28546819/