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