C#开发:openfiledialog的使用

1、 OpenFileDialog控件有以下基本属性

InitialDirectory对话框的初始目录
Filter要在对话框中显示的文件筛选器,例如,"文本文件(*.txt)|*.txt|所有文件(*.*)||*.*"
FilterIndex在对话框中选择的文件筛选器的索引,如果选第一项就设为1
RestoreDirectory控制对话框在关闭之前是否恢复当前目录
FileName第一个在对话框中显示的文件或最后一个选取的文件
Title将显示在对话框标题栏中的字符
AddExtension是否自动添加默认扩展名
CheckPathExists在对话框返回之前,检查指定路径是否存在
DefaultExt默认扩展名
DereferenceLinks在从对话框返回前是否取消引用快捷方式
ShowHelp启用"帮助"按钮
ValiDateNames控制对话框检查文件名中是否不含有无效的字符或序列

  2、 OpenFileDialog控件有以下常用事件

FileOk当用户点击"打开"或"保存"按钮时要处理的事件
HelpRequest当用户点击"帮助"按钮时要处理的事件

  

  可以用以下代码来实现上面这个对话框:

private void openFileDialogBTN_Click(object sender, System.EventArgs e){ OpenFileDialog openFileDialog=new OpenFileDialog(); openFileDialog.InitialDirectory="c:\\";//注意这里写路径时要用c:\\而不是c:\ openFileDialog.Filter="文本文件|*.*|C#文件|*.cs|所有文件|*.*"; openFileDialog.RestoreDirectory=true; openFileDialog.FilterIndex=1; if (openFileDialog.ShowDialog()==DialogResult.OK) { fName=openFileDialog.FileName; File fileOpen=new File(fName); isFileHaveName=true; richTextBox1.Text=fileOpen.ReadFile(); richTextBox1.AppendText(""); } }

  路径的返回用filename是字符串类型

如:openFileDialog1.ShowDialog(); _name1= openFileDialog1.FileName; Image imge = Image.FromFile(_name1);

 

 

为了方便同事在日常工作中很快速生成大量数据, 我做了文件拷贝的小工具:

其中用到了OpenFileDialog这个类,下面是关于这个类的一些用法!

OpenFileDialog类是用来选择文件位置的,

FolderBrowserDialog 类用来选择文件夹位置.

具体代码如下:

程序源码:

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO;

namespace copyFile {     public partial class Form1 : Form     {         String fileName;         String folderName;         String extendedName;         String fileName1;                 public Form1()         {             InitializeComponent();         }

        private void browse_Click(object sender,EventArgs e)         {             OpenFileDialog ofd = new OpenFileDialog();                //new一个方法             ofd.InitialDirectory = Environment. GetFolderPath_r(Environment.SpecialFolder.MyDocuments); //定义打开的默认文件夹位置             ofd.ShowDialog();          //显示打开文件的窗口              fileName = ofd.FileName;                //获得选择的文件路径              textBox1.Text = fileName;              extendedName = Path. GetExtension_r(fileName);       //获得文件扩展名              fileName1 = Path. GetFileName_r(fileName);           //获得文件名         }

        private void folder_Click(object sender, EventArgs e)         {             FolderBrowserDialog fbd = new FolderBrowserDialog();             fbd.ShowDialog();             folderName = fbd.SelectedPath;                     //获得选择的文件夹路径             textBox3.Text = folderName;         }

        private void ok_Click(object sender, EventArgs e)         {            &nbsp; if (textBox1.Text.Trim().Length == 0)             {                 MessageBox.Show("文件路径不能为空!");                 return;             }             if (textBox2.Text.Trim().Length == 0)             {                 MessageBox.Show("复制数量不能为空!");                 return;             }             if (textBox3.Text.Trim().Length == 0)             {                 MessageBox.Show("目标文件夹路径不能为空!");                 return;             }             String newFile;                   //定义存储的位置,和存储的名称                         for (int i = 1; i <= Convert.ToInt32(textBox2.Text); i++)                   //从textBox2中获取要复制的次数             {                 newFile = folderName + "\\" + fileName1 +"_"+ i.ToString() + extendedName;

      File.Copy(fileName, newFile, true);            //使用Copy复制文件, Copy(源文件位置,目标文件夹位置,是否可以覆盖同名文件)             }             MessageBox.Show("复制完成!");         }     } }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值