WPF 呼叫OpenFileDialog物件,實現獲取檔案上傳路勁
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;
}
}
}
前臺程式碼
<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>