如何在Windows应用程序中使用wpf和c#上传文件

时间:2014-06-03 06:32:33

标签: c# .net wpf windows

我的表单上有浏览按钮,当我点击它时,对话框打开,我选择了一个文件,现在该文件的路径或文件名将显示在文本框上,我已经编写了代码,但文件路径不是显示在文本框中,我已尝试解决但失败,请让我离开这个,还有一件事,我想上传到specfic文件夹然后在我的系统上下载,请告诉我这个也< / p>

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.IO;
//using System.Drawing;
using System.ComponentModel;



namespace RIS_2
{
    /// <summary>
    /// Interaction logic for crud.xaml
    /// </summary>
    public partial class crud : Window
    {
        public crud()
        {
            this.InitializeComponent();

            // Insert code required on object creation below this point.
        }

        private void browse_btn_Click(object sender, RoutedEventArgs e)
        {
            Stream checkStream = null;
         //   OpenFileDialog op1 = new OpenFileDialog();
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.Multiselect = false;
            dlg.Filter = "All Image Files | *.*";
            //dlg.Filter = "(*.pfw)|*.pfw|All files (*.*)|*.*";
           Nullable<bool> result = dlg.ShowDialog();
            // if ((bool)dlg.ShowDialog())
            if(result==true)
            {
                try
                {
                    if ((checkStream = dlg.OpenFile()) != null)
                    { 
                    //   MyImage.Source = new BitmapImage(new Uri(dlg.FileName, UriKind.Absolute));
                        //listBox1.Items.Add(openFileDialog.FileName);
                        string filename = dlg.FileName;
                        tb_file.Text = filename;
                       // tb_file.AppendText(dlg.FileName.ToString());
                        MessageBox.Show("Successfully done", filename);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
                }

            }
             else
             {

                 MessageBox.Show("Problem occured, try again later");

             }

        }


    }
}

1 个答案:

答案 0 :(得分:1)

坦率地说,对我而言,代码看起来还不错,所以它并不奇怪,它不起作用。话虽如此,我建议你采取不同的方法。您正在使用WPF,它非常擅长数据绑定。如果你在后面的代码中定义了一个属性

private string _fileName;
public string FileName
{
 get{return _fileName;}
 set{
  _fileName = value;
  OnPropertyChanged("FileName");
}

可以在http://www.java2s.com/Tutorial/CSharp/0470__Windows-Presentation-Foundation/RaisethePropertyChangedevent.htm找到OnPropertyChanged的实现和INotifyPropertyChanged的简单说明

在您的XAML中,您已经有一个TextBlock,它应该使用绑定

进行扩展
TextBlock x:Name="tb_file" Text="{Binding Path=Filename}"

要确保绑定到后面代码中的属性,请参阅Binding objects defined in code-behind

希望有帮助(至少问题的第一部分)