zoukankan      html  css  js  c++  java
  • OpenFileDialog

    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);

  • 相关阅读:
    「2019.7.25 考试」偶然发生
    「刷题」可怜与STS
    「刷题」小星星
    「刷题」数三角形
    「刷题」 关于线段上的整点个数
    「刷题」Color 群论
    「2019.7.22 考试」AC和WA0一步之遥
    「刷题」幸运数字
    「刷题」卡特兰数&prufer序列
    「刷题」一个人的数论
  • 原文地址:https://www.cnblogs.com/dyufei/p/2573938.html
Copyright © 2011-2022 走看看