Visual Studio 中用代码打开文件目录
2014-04-24 13:46
309 查看
首先引用 命名空间: System.Windows.Forms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace OpenFile
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Stream myStream = null;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "c:\\";
openFileDialog1.Filter = "Excel 文件t(*.xl*;*.xlsx;*.xlsm;*.xlsb;*.xlam;*.xltx;*.xltm;*.xls)|*.xl*;*.xlsx;*.xlsm;*.xlsb;*.xlam;*.xltx;*.xltm;*.xls|All files (*.*)|*.*";
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
if ((myStream = openFileDialog1.OpenFile()) != null)
{
using (myStream)
{
// Insert code to read the stream here.
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
}
}
}
}
实现效果:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace OpenFile
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Stream myStream = null;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "c:\\";
openFileDialog1.Filter = "Excel 文件t(*.xl*;*.xlsx;*.xlsm;*.xlsb;*.xlam;*.xltx;*.xltm;*.xls)|*.xl*;*.xlsx;*.xlsm;*.xlsb;*.xlam;*.xltx;*.xltm;*.xls|All files (*.*)|*.*";
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
if ((myStream = openFileDialog1.OpenFile()) != null)
{
using (myStream)
{
// Insert code to read the stream here.
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
}
}
}
}
实现效果:
相关文章推荐
- asp.net 2.0 中用C#代码在C:/Inetpub/wwwroot下创建目录,创建文件,写文件等有权限问题,可以用FSO方式
- 尝试为文件 F:\visual studio 2010\bkbs\paperCMS\App_Data\Project.mdf 附加自动命名的数据库,但失败。已存在同名的数据库,或指定的文件无法打开或位于 UNC 共享目录中。
- visual studio打开文件所在目录
- VB6.0新手入门代码 用VB打开程序所在目录下的文件
- 通过Java代码打开浏览器,本地文件目录以及ftp站点
- 【VS插件开发】在Visual Studio中打开文件,并定位到代码行
- Visual Studio中将打开的代码与类文件进行关联
- Visual Studio 2010工程目录下的ipch文件夹和.sdf文件
- Eclipse中打开文件目录或命令行的工具:startexplorer
- Java 通过调用系统程序打开文件或者目录的方式
- 明明在包含目录里包含了cv.h的路径,为什么还是报错error C1083: 无法打开包括文件: “opencv/cv.h”: No such file or directory
- Makefile中用foreach,eval,call实现将目标文件编译到指定目录
- Python实现读取目录所有文件的文件名并保存到txt文件代码
- vs2008打开aspx文件时设计界面死机的解决(Visual Studio 2008切换设计视图假死正在安装)
- 17joys代码编写开始-项目目录、入口文件建立
- Qt-编程模块 001: 浏览(选择)、打开 文件和文件夹(目录) --- 如何让按钮**记忆上次打开的文本路径**的功能
- PHPStorm打开文件所在目录
- EclipseIDE制作<打开文件所在目录>插件 for windows
- source insight实现快捷键打开文件目录
- MFC Doc/View笔记(一) 打开文件代码跟踪