2011-02-02 546 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> 

在您的視圖模型聲明一個命令,並將其綁定在視圖正如我裏面按鈕來完成。現在,一旦用戶點擊按鈕,您將在代碼中獲得控制權。在該代碼中創建一個窗口並啓動它。一旦用戶關閉窗口,閱讀內容並做任何你想要的。

相關問題