WPF 调用OpenFileDialog对象,实现获取文件上传路劲

时间:2022-06-29 10:58:05

wpf  好像 没有直接的文件上传控件,反正我没找到

我在网上找了好久,没有简单直接的例子,只好自己放了一个按钮,然后调用OpenFileDialog对象,实现获取文件上传路劲

 

后台代码

 private void button1_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.OpenFileDialog ofld = new System.Windows.Forms.OpenFileDialog();
            if (ofld.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string text = ofld.FileName;
                //string s = System.IO.Path.GetFileNameWithoutExtension(ofld.FileName).ToString();
                if (text != "" || text != null)
                {
                    this.tbPath.Text = text;
                }
               
            }
        }

 

前台代码

<Page x:Class="MyAppWpfApplication.MyAppDefinePage"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:loc="clr-namespace:mgen_fileSelector1"
      mc:Ignorable="d"
      d:DesignHeight="360" d:DesignWidth="738"
 Title="MyAppDefinePage" xmlns:my="clr-namespace:MyAppWpfApplication.UC">

    <Grid Width="742" Height="358">
        <Grid.RowDefinitions>
            <RowDefinition Height="150*" />
            <RowDefinition Height="208*" />
        </Grid.RowDefinitions>

        <GroupBox Header="应用" HorizontalAlignment="Left" Margin="12,4,0,0" Name="groupBox1" Width="532">
            <Grid Height="122" Width="505">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="0*" />
                    <ColumnDefinition Width="505*" />
                </Grid.ColumnDefinitions>
                <Button Content="..." Grid.Column="1" Height="23" HorizontalAlignment="Left" Margin="392,47,0,0" Name="button1" VerticalAlignment="Top" Width="24" Click="button1_Click" />
                <Label Content="path:" Grid.Column="1" Height="28" HorizontalAlignment="Left" Margin="15,46,0,0" Name="lblPath" VerticalAlignment="Top" />
                <TextBox Grid.Column="1" Height="23" HorizontalAlignment="Left" Margin="60,48,0,0" Name="tbPath" VerticalAlignment="Top" Width="330"/>
               
            </Grid>
        </GroupBox>
    </Grid>
</Page>