关于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. ANNOTATION and analyse hello1.java

    一.What is annotation? annotation的中文意思就是注解,注释的意思.注解也属于一种类型.它是在 Java SE 5.0 版本中开始引入的概念.它的形式跟接口很类似,不过前面 ...

  2. 打造个人的vimIDE

    环境说明 系统版本:centos7.Ubuntu16 vim版本:7.4 安装git工具 整体说明:本文的vim配置是针对Linux的单个系统用户,python的自动补全使用的是 jedi-vim 插 ...

  3. C语言中指针的加减运算

    参考文章,值得一看 char arr[3]; printf("arr:\n%d\n%d\n%d\n", arr, arr + 1, arr + 2); char *parr[3]; ...

  4. iframe使用大全

    <iframe src=”you page’s url” width=”100″ height=”30″ frameborder=”no” border=”0″ marginwidth=”0″ ...

  5. 【Codeforces 644A】Parliament of Berland

    [链接] 我是链接,点我呀:) [题意] 题意 [题解] https://blog.csdn.net/V5ZSQ/article/details/70873661 看这个人的吧. [代码] #incl ...

  6. JavaSE 学习笔记之网络编程(二十三)

    端口: 物理端口: 逻辑端口:用于标识进程的逻辑地址,不同进程的标识:有效端口:0~65535,其中0~1024系统使用或保留端口. java 中ip对象:InetAddress. import ja ...

  7. nyoj 113 字符串替换 (string中替换函数replace()和查找函数find())

    字符串替换 时间限制:3000 ms  |  内存限制:65535 KB 难度:2   描述 编写一个程序实现将字符串中的所有"you"替换成"we"   输入 ...

  8. 《Noisy Activation Function》噪声激活函数(一)

    本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/51736830 Noisy Activa ...

  9. github & Front-end JavaScript frameworks

    github & Front-end JavaScript frameworks https://github.com/collections/front-end-javascript-fra ...

  10. HDU 4902 (牛叉的线段树)

    Nice boat Problem Description There is an old country and the king fell in love with a devil. The de ...