2011-02-15 128 views

回答

86

这些链接举例

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. 
} 
+0

非常感谢你的快速回复....它帮了我很多.. – Harikasai 2011-02-16 04:34:41

35
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 
} 
17
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 ; 
} 

在此代码解释它,你可以把你的地址在文本框中。

相关问题