用c#实现:输入一个文件的绝对地址,判断这个文件是否存在,使用wpf中的Textbox和Button完成,要有具体的步骤和代码。
时间: 2024-02-03 10:14:22 浏览: 97
好的,以下是具体的步骤和代码。
步骤:
1. 在WPF窗口中添加一个TextBox和一个Button控件。
2. 在Button的Click事件中编写代码,判断文件是否存在。
3. 使用MessageBox提示文件是否存在。
代码:
XAML代码:
```
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="150" Width="400">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Label Content="输入文件路径:" Grid.Row="0" Margin="5"/>
<TextBox x:Name="txtFilePath" Grid.Row="1" Margin="5" />
<Button Content="检查文件是否存在" Grid.Row="2" Margin="5" Click="Button_Click" />
</Grid>
</Window>
```
C#代码:
```
private void Button_Click(object sender, RoutedEventArgs e)
{
string filePath = txtFilePath.Text;
if (File.Exists(filePath))
{
MessageBox.Show("文件存在!");
}
else
{
MessageBox.Show("文件不存在!");
}
}
```
在Button的Click事件中,首先获取TextBox中输入的文件路径,然后使用File.Exists方法判断文件是否存在,最后使用MessageBox显示结果。
阅读全文
相关推荐
















