1. OpenFileDialog的使用
//open the dialogprivate void butn_open_Click(object sender, EventArgs e){openFileDialog1.InitialDirectory = "D:\\"; // 这里是初始的路径名openFileDialog1.Filter = "类文件|*.cs|所有文件|*.*"; //设置打开文件的类型openFileDialog1.RestoreDirectory = true; //设置是否还原当前目录openFileDialog1.FilterIndex = 0; //设置打开文件类型的索引string path = ""; //用于保存打开文件的路径if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK){path = openFileDialog1.FileName;MessageBox.Show(path); //显示该路径名}}
2.FolderBrowserDialog 的使用
//open the dialogprivate void butn_open_Click(object sender, EventArgs e){folderBrowserDialog1.RootFolder = Environment.SpecialFolder.Desktop; // 这里是设置初始的路径名string path = ""; //保存选择文件夹的名称if (folderBrowserDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK){path = folderBrowserDialog1.SelectedPath;MessageBox.Show(path); //显示该路径名}}