OpenFileDialog 用于浏览并打开文件,在Windows Forms中使用,表现为标准的Windows对话框。

实例:

1.新建Windows Form Application

2.添加OpenFileDialog

打开Toolbox,找到并双击OpenFileDialog:

可以在窗口下方看到添加到OpenFileDialog.

3.添加按钮和事件

OpenFileDialog对话框需要通过事件激活,添加一个Button到Form中,双击Button以添加事件,事件代码如下:

using System;
using System.Windows.Forms; namespace OpenFileDialog {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
} private void button1_Click(object sender, EventArgs e) {
// show the dialog and get result
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
}
Console.WriteLine(result);
}
}
}

4.读取文件

可以通过OpenFileDialog读取文件,修改按钮的点击事件,具体代码如下:

using System;
using System.IO;
using System.Windows.Forms; namespace OpenFileDialog {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
int size = -1;
// show the dialog and get result
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
string file = openFileDialog1.FileName;
try
{
string text = File.ReadAllText(file);
size = text.Length;
}
catch (Exception)
{
throw;
}
}
Console.WriteLine(result);
Console.WriteLine(size); // 文件大小
}
}
}

编译运行,点击button1,会显示OpenFileDialog,然后验证DialogResult,通过File.ReadAllText读写文件,然后获得文件大小。

5. 属性

属性 说明
AddExtension 扩展名是否添加到文件名,默认为真,如果想自动修改文件扩展名,可将其设置为false。
AutoUpgradeEnabled 用于获得Vista风格的打开文件对话框,默认为true,推荐使用。
DefaultExt 默认文件扩展名,如果文件扩展名没有指定,则自动添加该扩展
DereferenceLinks 从对话框返回路径前是否解引用快捷键
FileName 在对话框最开始显式的文件名
InitialDirectory 对话框的初始目录
Multiselect 是否一次可选择多个文件,可通过SHIFT或CTRL键进行多项选择

6.Filter

Filters能帮助有效的筛选文件,OpenFileDialog支持文件名过滤,可用*代表任意字符。

Filter:

用于指定过滤器,如:“C# files|*.cs”,此时只显式以”.cs”结尾的文件

FilterIndex:

用于指定默认Filter,其Index为1.其后的filter的Index依次递增

ValidateNames:

Windows文件系统不允许文件名包含特定字符如”*”,该选项一般为True。

7.ReadOnly

OpenFileDialog部分属性可允许指定文件是否为只读。可以显式read-only checkbox。大多时候用不着.

ReadOnlyChecked:

用于设置”read only”复选框的值,仅当”ShowReadOnly”设置为True才可见。

ShowReadOnly:

“Read-only”复选框是否可见。

C# OpenFileDialog的更多相关文章

  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. OpenFileDialog使用方法

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

  9. C# OpenFileDialog和PictrueBox

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

随机推荐

  1. Android启动画面实现

    每个Android应用程序启动之后都会出现一个Splash启动界面,显示产品LOGO.公司LOGO或者开发者信息.如果应用程序启动时间比较长,那么启动界面就是一个很好的东西,可以让用户耐心等待这段枯燥 ...

  2. 使用eclipse和maven生成java web程序war包

    一.eclipse中,在需要打包的项目名上右击,然后把鼠标光标指向弹出框中的“run as”: 二.之后会看到在这个弹出框的右侧会出现一个悬浮窗,如下: 三.在上边的第二个悬浮窗鼠标点击“maven  ...

  3. Akka(一) - akka的wordcount

    1. 启动类 object Application extends App{ val _system = ActorSystem("HelloAkka") //构建akka容器 v ...

  4. CentOS of MySQL command

    1.本地连接数据库 [root@iZ253lxv4i0Z mysql]# mysql -u root -pEnter password: or: [root@iZ253lxv4i0Z mysql]# ...

  5. BIP_开发案例01_BI Publisher报表手工提交和控制(案例)

    2014-12-27 Created By BaoXinjian

  6. OAF_EO系列6 - Delete详解和实现(案例)

    2014-06-14 Created By BaoXinjian

  7. 在sql脚本中将查询结果集拼接成字符串

  8. MDK+硬件仿真器实现debugprintf()-stm32

    MDK+硬件仿真器实现debugprintf()-stm32 1MDK工程设置如下 2其中stm32debug.ini文件内容为 /********************************** ...

  9. RPC介绍

    转载http://blog.csdn.net/mindfloating/article/details/39474123/ 近几年的项目中,服务化和微服务化渐渐成为中大型分布式系统架构的主流方式,而 ...

  10. python第一个hello world注意问题!!

    如果你第一次写python代码,想写一个通常的hello world ,那么你需要注意这个hello world的写法,这和python的版本有直接关系!!! Python 3.x: print('h ...