DYF
我思故我在!

OpenFileDialog 组件可以浏览本机或网络中任何计算机上的文件夹,并选择打开一个或多个文件,返回用户在对话框中选定的文件的路径和名称。

用户选定要打开的文件后,可以使用两种机制来打开文件。

如果希望使用文件流,则可以创建 StreamReader 类的实例。另一种方法是使用 OpenFile 方法打开选定的文件。

示例1

private void button1_Click(object sender, System.EventArgs e)
{
if(openFileDialog1.ShowDialog() == DialogResult.OK)
{
System.IO.StreamReader sr = new
System.IO.StreamReader(openFileDialog1.FileName);
MessageBox.Show(sr.ReadToEnd());
sr.Close();
}
}

在窗体的构造函数中放置以下代码,以注册事件处理程序。

this.button1.Click += new System.EventHandler(this.button1_Click);

示例2

private void btnBrowse_Click(object sender, EventArgs e)

{

OpenFileDialog OpenPhotoFileDialog = new OpenFileDialog();

OpenPhotoFileDialog.Filter = "jpg files (*.jpg)|*.jpg|bmp files(*.bmp)|*.bmp|png files (*.png)|*.png|All files (*.*)|*.*";

OpenPhotoFileDialog.ShowDialog();

if (OpenPhotoFileDialog.FileName.Trim() != "")

{

this.pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;

this.pictureBox1.ImageLocation = OpenPhotoFileDialog.FileName;

this.txtPhotoFilePath.Text = OpenPhotoFileDialog.FileName;

}

}

示例3


private void button1_Click(object sender, System.EventArgs e)
{
// Displays an OpenFileDialog so the user can select a Cursor.
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "Cursor Files|*.cur";
openFileDialog1.Title = "Select a Cursor File";
// Show the Dialog.
// If the user clicked OK in the dialog and
// a .CUR file was selected, open it.
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
// Assign the cursor in the Stream to the Form's Cursor property.
this.Cursor = new Cursor(openFileDialog1.OpenFile());
}
}

在窗体的构造函数中放置以下代码,以注册事件处理程序。

this.button1.Click += new System.EventHandler(this.button1_Click);

posted on 2010-03-26 17:58  o(∩_∩)o...  阅读(860)  评论(0编辑  收藏  举报