C#中窗体中打开一个openFileDialog,选中一个文件后,点击"打开"按钮,获得选中文件的名称

2024-11-25 23:29:44
推荐回答(2个)
回答1:

在Button1的Click事件里,添加下面的代码
OpenFileDialog dialog = new OpenFileDialog();
dialog.ShowDialog();
if (!string.IsNullOrEmpty(dialog.FileName))
{
button2.Text = dialog.FileName;
}

回答2:

OpenFileDialog openFile = new OpenFileDialog();
if (openFile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
MessageBox.Show("文件路径:"+openFile.FileName+"文件名:"+openFile.SafeFileName);
}