在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; }
如果想让图片显示出来,可以使用Image控件
// 显示图片 BitmapImage image = new BitmapImage( new Uri(txtUpLoadHeart.Text, UriKind.Absolute)); imgHeart.Source = image;