如何使用C#将浏览文件button添加到Windows窗体

我想单击“浏览”button时select本地硬盘上的文件。

我不知道如何使用OpenFileDialog控件。 谁能帮我?

这些链接用例子来解释

http://dotnetperls.com/openfiledialog

http://www.geekpedia.com/tutorial67_Using-OpenFileDialog-to-open-files.html

 private void button1_Click(object sender, EventArgs e) { int size = -1; DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog. if (result == DialogResult.OK) // Test result. { string file = openFileDialog1.FileName; try { string text = File.ReadAllText(file); size = text.Length; } catch (IOException) { } } Console.WriteLine(size); // <-- Shows file size in debugging mode. Console.WriteLine(result); // <-- For debugging use. } 
 var FD = new System.Windows.Forms.OpenFileDialog(); if (FD.ShowDialog() == System.Windows.Forms.DialogResult.OK) { string fileToOpen = FD.FileName; System.IO.FileInfo File = new System.IO.FileInfo(FD.FileName); //OR System.IO.StreamReader reader = new System.IO.StreamReader(fileToOpen); //etc } 
 OpenFileDialog fdlg = new OpenFileDialog(); fdlg.Title = "C# Corner Open File Dialog" ; fdlg.InitialDirectory = @"c:\" ; fdlg.Filter = "All files (*.*)|*.*|All files (*.*)|*.*" ; fdlg.FilterIndex = 2 ; fdlg.RestoreDirectory = true ; if(fdlg.ShowDialog() == DialogResult.OK) { textBox1.Text = fdlg.FileName ; } 

在这个代码中,你可以把你的地址放在一个文本框中。