C# 打开对话框实例(详)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace TestFile
{
    public partial class Form1 : Form
    {
        OpenFileDialog ofd;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ofd = new OpenFileDialog();
            ofd.InitialDirectory = "";  //对话框打开初始目录
            ofd.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";   // Filter,FilterIndex一定要写在ShowDialog()之前,否则无效
            ofd.FilterIndex = 1;
            ofd.FileName = "abc";   // 设置一个包含在文件对话框中选定的文件名的字符串,查找文件名为abc
            ofd.CheckFileExists = true;  // 在对话框返回之前,检查指定路径是否存在,默认为true
            ofd.InitialDirectory = "c://";//对话框的初始目录
            ofd.ShowDialog();
            String strOpenPath = ofd.FileName;  //关闭文件对话框之后,获取用户选择的完整路径名
            Console.WriteLine(strOpenPath);   
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                if (ofd.FileName != "")
                {
                    MessageBox.Show("你选择了" + ofd.FileName);
                }
            }

        }

 

    }
}

发布了25 篇原创文章 · 获赞 1 · 访问量 2万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章