关于OpenFileDialog的使用

(2010-03-25 12:51:33)

标签:

杂谈

分类: WinForm

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)
        {
             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("复制完成!");
        }
    }
}

补充:

//获取文件名
Path. GetFileName_r(OpenFileDialog.FileName)

//获取文件路径
Path. GetDirectoryName_r(OpenFileDialog.FileName)

//获取文件扩展名
Path. GetExtension_r(OpenFileDialog.FileName)

关于OpenFileDialog的使用 2(转)的更多相关文章

  1. 1、怎样设置C#OpenFileDialog(文件选择窗体)的指定路径、文件格式等属性(设置打开默认路径、文件格式、窗体显示文本)

    C#的OpenFileDialog的常用属性设置 1.设置属性 1)设置弹出的指定路径(绝对路径.相等路径) 2)设置标题 3)设置文本格式 2.打开方式1(绝对路径) 2.1) 打开的路径

  2. Dev 关于用openFileDialog控件上传图片的问题

    1. OpenFileDialog控件有以下基本属性 InitialDirectory 对话框的初始目录 Filter 要在对话框中显示的文件筛选器,例如,"文本文件(*.txt)|*.tx ...

  3. C# winform OpenFileDialog MessageBox

    1.弹出窗体选择本地文件-OpenFileDialog OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Tit ...

  4. OpenFileDialog获取文件名和文件路径问题

    OpenFileDialog获取文件名和文件路径问题(转) 转自:http://blog.sina.com.cn/s/blog_7511914e0101cbjn.html System.IO.Path ...

  5. 使用OpenFileDialog会更改默认程序目录

    这个问题可能只有在特定的程序中会发现:当我们在程序中使用相对路径时是依赖于当前目录的.所以在使用类似代码: XElement rootNode = XElement.Load(@"zips/ ...

  6. OpenFileDialog - 设置 - Filter 笔记

    using (OpenFileDialog fileSelectDialog = new OpenFileDialog()) { fileSelectDialog.Multiselect = fals ...

  7. C#的OpenFileDialog和SaveFileDialog的常见用法(转)

    OpenFileDialog openFileDialog1 = new OpenFileDialog();            openFileDialog1.InitialDirectory = ...

  8. C# OpenFileDialog

    OpenFileDialog 用于浏览并打开文件,在Windows Forms中使用,表现为标准的Windows对话框. 实例: 1.新建Windows Form Application 2.添加Op ...

  9. OpenFileDialog使用方法

    OpenFileDialog基本属性 AddExtension 控制是否将扩展名自动添加到文件名上 CheckFileExists 指示用户指定不存在的文件时是否显示警告 CheckPathExist ...

  10. C# OpenFileDialog和PictrueBox

     string resultFile = ""; OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFile ...

随机推荐

  1. Xamarin View获取属性的绑定信息

    public static Binding GetBinding( BindableObject self, BindableProperty property) { var methodInfo = ...

  2. 16位/32位/64位CPU的位究竟是说啥

    平时,我们谈论CPU,都会说某程序是32位编译,可以跑在32位机或64位机,或则是在下载某些开源包时,也分32位CPU版本或64CPU位版本,又或者在看计算机组成相关书籍时,特别时谈到X86 CPU时 ...

  3. TWaver3D特效之高光反射

    前篇我们介绍了TWaver 3D的环境映射特效,下面我们接着给大家分享高光反射特效.高光反射定义了物体上的某一区域比其他地方更反光.在高光反射的贴图中,黑色区域的反射率为0(完全不反光),白色区域的反 ...

  4. 干货分享--iOS及Mac开源项目和学习资料【超级全面】

    原文出处:codecloud http://www.kancloud.cn/digest/ios-mac-study/84557

  5. linux which-查找并显示给定命令的绝对路径

    推荐:更多Linux 文件查找和比较 命令关注:linux命令大全 which命令用于查找并显示给定命令的绝对路径,环境变量PATH中保存了查找命令时需要遍历的目录.which指令会在环境变量$PAT ...

  6. CCF201612-2 工资计算 java(100分)

    试题编号: 201612-2 试题名称: 工资计算 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 小明的公司每个月给小明发工资,而小明拿到的工资为交完个人所得税之后的工资.假 ...

  7. BNUOJ 19792 Airport Express

    Airport Express Time Limit: 1000ms Memory Limit: 131072KB This problem will be judged on UVA. Origin ...

  8. 给nginx生成自签名证书

    https://blog.csdn.net/qq_26819733/article/details/53431662 https://www.liaoxuefeng.com/article/00141 ...

  9. 项目中应用到的框架和技术之二——ol3-ext

    ol3-ext有很多很丰富的效果,可以不用重复造轮子,ol3-ext示例大全:http://viglino.github.io/ol3-ext/ 在本次项目中使用到了ol3-ext的两个功能:图层管理 ...

  10. Node & Express: some tips

    1. 设置Express端口号: 在app.js中添加 app.set('port', process.env.PORT || 3000); 之后命令行中打入 PORT=1234 node app.j ...