2011-02-02 545 views

回答

7

您可以使用OpenFileDialog类来获取文件选择对话框

OpenFileDialog fileDialog= new OpenFileDialog(); 
fileDialog.DefaultExt = ".txt"; // Required file extension 
fileDialog.Filter = "Text documents (.txt)|*.txt"; // Optional file extensions 

fileDialog.ShowDialog(); 

阅读的内容:您将获得从打开文件对话框的文件名和使用做就可以了什么IO操作。

if(fileDialog.ShowDialog() == DialogResult.OK) 
    { 
    System.IO.StreamReader sr = new 
    System.IO.StreamReader(fileDialog.FileName); 
    MessageBox.Show(sr.ReadToEnd()); 
    sr.Close(); 
    } 
+2

`DialogResult.OK`不存在,但我使用`bool? res = fileDialog.ShowDialog();如果(res.HasValue && res.Value){...}` – wormsparty 2011-08-31 14:26:36

2
<StackPanel Orientation="Horizontal"> 
    <TextBox Width="150"></TextBox> 
    <Button Width="50" Content="Browse" Command="{Binding Path=CommandInViewModel}"></Button> 
</StackPanel> 

在您的视图模型声明一个命令,并将其绑定在视图正如我里面按钮来完成。现在,一旦用户点击按钮,您将在代码中获得控制权。在该代码中创建一个窗口并启动它。一旦用户关闭窗口,阅读内容并做任何你想要的。

相关问题