标签:
//打开文件对话框 private void openFileDialogBtn_Click(object sender, EventArgs e) { OpenFileDialog opd = new OpenFileDialog(); opd.InitialDirectory = @"D:\"; //对话框初始路径 opd.Filter = "C#文件(*.cs)|*.cs|文本文件(*.txt)|*.txt|所有文件(*.*)|*.*"; opd.FilterIndex = 2; //默认就选择在文本文件(*.txt)过滤条件 opd.DereferenceLinks = false; //返回快捷方式的路径而不是快捷方式映射的文件的路径 opd.Title = "打开对话框"; opd.RestoreDirectory = true; //每次打开都回到InitialDirectory设置的初始路径 opd.ShowHelp = true; //对话框多了个"帮助"按钮 opd.ShowReadOnly = true; //对话框多了"只读打开"的复选框 opd.ReadOnlyChecked = true; //默认"只读打开"复选框勾选 opd.HelpRequest +=new EventHandler(opd_HelpRequest); //注册帮助按钮的事件 // opd.FileOk += new CancelEventHandler(opd_FileOk); //效果等同于下面 if(opd.ShowDialog() == DialogResult.OK) { string filePath = opd.FileName; //文件路径 string fileName = opd.SafeFileName; //文件名 } } private void opd_FileOk(object sender, CancelEventArgs e) { //do something } /// <summary> /// 帮助按钮单击事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void opd_HelpRequest(object sender, EventArgs e) { MessageBox.Show("这是帮助说明"); }
//保存文件对话框 private void saveFileDialogBtn_Click(object sender, EventArgs e) { SaveFileDialog sfd = new SaveFileDialog(); sfd.InitialDirectory = @"D:\"; //对话框初始路径 sfd.FileName = "config.txt"; //默认保存的文件名 sfd.Filter = "C#文件(*.cs)|*.cs|文本文件(*.txt)|*.txt|所有文件(*.*)|*.*"; sfd.FilterIndex = 2; //默认就选择在文本文件(*.txt)过滤条件上 sfd.DefaultExt = ".xml"; //默认保存类型,如果过滤条件选"所有文件(*.*)"且保存名没写后缀,则补充上该默认值 sfd.DereferenceLinks = false; //返回快捷方式的路径而不是快捷方式映射的文件的路径 sfd.Title = "保存对话框"; sfd.RestoreDirectory = true; //每次打开都回到InitialDirectory设置的初始路径 sfd.ShowHelp = true; //对话框多了个"帮助"按钮 sfd.HelpRequest +=new EventHandler(sfd_HelpRequest); //注册帮助按钮的事件 // sfd.FileOk += new CancelEventHandler(sfd_FileOk); //效果等同于下面 if(sfd.ShowDialog() == DialogResult.OK) { string filePath = sfd.FileName; //文件路径 } } private void sfd_FileOk(object sender, CancelEventArgs e) { //do something } /// <summary> /// 帮助按钮单击事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void sfd_HelpRequest(object sender, EventArgs e) { MessageBox.Show("这是帮助说明"); }
//打开文件夹对话框 private void folderBrowserDialogBtn_Click(object sender, EventArgs e) { FolderBrowserDialog fbd = new FolderBrowserDialog(); fbd.RootFolder = Environment.SpecialFolder.Desktop; //设置默认根目录是桌面 fbd.Description = "请选择文件目录:"; //设置对话框说明 if (fbd.ShowDialog() == DialogResult.OK) { string filePath = fbd.SelectedPath; } }
标签:
原文地址:http://www.cnblogs.com/luciakally/p/4644511.html