wpf 如何在windows应用程序中使用wpf和c#上传文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24008499/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to upload file using wpf and c# in windows application
提问by user3667841
I have form on which there is browse button , when i clicked it, dialogue box open and i selected a file now the path or file name of that file is to be show on textbox i have written code but file path is not display in text box, i have try to resolve but fail, kindly get me out of this, and there is one thing more, i want to upload it in specfic folder and then download it on my system , please tell me about this also
我有一个表单,上面有浏览按钮,当我点击它时,对话框打开,我现在选择了一个文件,该文件的路径或文件名将显示在文本框中我已经编写了代码,但文件路径未以文本显示框,我已尝试解决但失败了,请让我摆脱这种情况,还有一件事,我想将其上传到特定文件夹中,然后将其下载到我的系统上,也请告诉我这件事
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");
}
}
}
}
回答by SimonAx
Frankly, to me the code looks okay, so it's strange that it doesn't work. Having said that, I'd suggest that you take a different approach. You are using WPF, which is very good at data binding. If you in the code behind define a property
坦率地说,对我来说代码看起来不错,所以奇怪的是它不起作用。话虽如此,我建议您采取不同的方法。您正在使用 WPF,它非常擅长数据绑定。如果你在代码后面定义了一个属性
private string _fileName;
public string FileName
{
get{return _fileName;}
set{
_fileName = value;
OnPropertyChanged("FileName");
}
The implementation of OnPropertyChanged and a simple explanation of INotifyPropertyChanged can be found at http://www.java2s.com/Tutorial/CSharp/0470__Windows-Presentation-Foundation/RaisethePropertyChangedevent.htm
OnPropertyChanged 的实现和 INotifyPropertyChanged 的简单解释可以在http://www.java2s.com/Tutorial/CSharp/0470__Windows-Presentation-Foundation/RaisethePropertyChangedevent.htm找到
In your XAML, you already have a TextBlock which should be extended with a binding
在您的 XAML 中,您已经有一个 TextBlock,它应该使用绑定进行扩展
TextBlock x:Name="tb_file" Text="{Binding Path=Filename}"
To ensure that the binding to a property in the code behind works, please refer to Binding objects defined in code-behind
为确保绑定到后台代码中的属性有效,请参阅代码隐藏中定义的绑定对象
Hope that helps (at least the first part of your question)
希望有所帮助(至少是您问题的第一部分)

