在web程序中,我們對文件的瀏覽很簡單,微軟直接提供了一個Upload控件,或者使用File元素也可以,可以直接獲得路徑,但是在窗體應用程序中,卻沒有了,需要我們自定義這樣一個控件,通過提供的OpenFileDialog類來實現
首先,我們可以放一個按鈕,用來瀏覽文件,在放上一個textbox控件,用來顯示路徑
然后,就可以觸發按鈕事件
///
<summary>
/// 選擇本地現有的網頁
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnSelectUrl_Click( object sender, RoutedEventArgs e)
{
OpenFileDialog op = new Microsoft.Win32.OpenFileDialog();
op.InitialDirectory = lblSavePath.Text; // 默認的打開路徑
op.RestoreDirectory = true;
op.Filter = " 網頁文件(*.htm)|*.htm|*.html|(*.html)|文本文件(*.txt)|*.txt|所有文件(*.*)|*.* ";
op.ShowDialog();
txtLocalUrl.Text = op.FileName;
}
/// 選擇本地現有的網頁
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnSelectUrl_Click( object sender, RoutedEventArgs e)
{
OpenFileDialog op = new Microsoft.Win32.OpenFileDialog();
op.InitialDirectory = lblSavePath.Text; // 默認的打開路徑
op.RestoreDirectory = true;
op.Filter = " 網頁文件(*.htm)|*.htm|*.html|(*.html)|文本文件(*.txt)|*.txt|所有文件(*.*)|*.* ";
op.ShowDialog();
txtLocalUrl.Text = op.FileName;
}
如果想讓圖片顯示出來,可以使用Image控件
//
顯示圖片
BitmapImage image = new BitmapImage( new Uri(txtUpLoadHeart.Text, UriKind.Absolute));
imgHeart.Source = image;
BitmapImage image = new BitmapImage( new Uri(txtUpLoadHeart.Text, UriKind.Absolute));
imgHeart.Source = image;