private void button1_Click(object sender, EventArgs e) { OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.Title = "C# Corner Open File Dialog"; openFileDialog1.InitialDirectory = @"c:"; openFileDialog1.Filter = "All files(*.*)|*.*|All files(*.*)|*.* "; /* * FilterIndex 属性用于选择了何种文件类型,缺省设置为0,系统取Filter属性设置第一项 * ,相当于FilterIndex 属性设置为1.如果你编了3个文件类型,当FilterIndex =2时是指第2个. */ openFileDialog1.FilterIndex = 2; /* *如果值为false,那么下一次选择文件的初始目录是上一次你选择的那个目录, *不固定;如果值为true,每次打开这个对话框初始目录不随你的选择而改变,是固定的 */ openFileDialog1.RestoreDirectory = true; if (openFileDialog1.ShowDialog() == DialogResult.OK) { textBox1.Text = System.IO.Path.GetFileNameWithoutExtension(openFileDialog1.FileName); //System.IO.Path.GetFullPath(openFileDialog1.FileName); //绝对路径 //System.IO.Path.GetExtension(openFileDialog1.FileName); //文件扩展名 //System.IO.Path.GetFileNameWithoutExtension(openFileDialog1.FileName); //文件名没有扩展名 //System.IO.Path.GetFileName(openFileDialog1.FileName); //得到文件 //System.IO.Path.GetDirectoryName(openFileDialog1.FileName); //得到路径 } }