winform 实现选择文件和选择文件夹对话框的简单实例
程序员文章站
2022-11-21 11:37:04
实例如下:
//选择文件,点击【浏览】,选择文件
private void button1_click(object sender, eventargs e)...
实例如下:
//选择文件,点击【浏览】,选择文件 private void button1_click(object sender, eventargs e) { openfiledialog openfiledialog1 = new openfiledialog(); //显示选择文件对话框 openfiledialog1.initialdirectory = "c:\\"; openfiledialog1.filter = "txt files (*.txt)|*.txt|all files (*.*)|*.*"; openfiledialog1.filterindex = 2; openfiledialog1.restoredirectory = true; if (openfiledialog1.showdialog() == dialogresult.ok) { this.textbox1.text = openfiledialog1.filename; //显示文件路径 } } //选择文件夹:点击【浏览】,选择文件夹 private void button4_click(object sender, eventargs e) { if (this.folderbrowserdialog1.showdialog() == dialogresult.ok) { if (this.folderbrowserdialog1.selectedpath.trim() != "") this.textbox4.text = this.folderbrowserdialog1.selectedpath.trim(); } }
选择文件夹对话框,如果想默认一个文件夹,在click事件一开始添加以下代码:
folderbrowserdialog1.selectedpath =“d:\”;
呵呵,是不是很简单呢。
以上这篇winform 实现选择文件和选择文件夹对话框的简单实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。