如何读取资源 txt 文件的文本并将其放入 C# .net 4.0 WPF 中的 TextBox?我的表单中有一个 TextBox,资源文件夹中有一个 TXT 文件,如何将 TXT 文件中的文本(包括换行符、双空格等)放入 TextBox?
请为我简化代码,因为我是 C# 的初学者...谢谢!
无需提供代码,对于这个确切的问题,已经有一个非常简单的 WPF 示例,在此处找到。
顺便说一句,不要问“嘿,你们能帮我解决这个问题吗!” 你应该问更多的问题,比如“嘿,我试过这个,那个。为什么这不起作用,有什么问题。我还在某处读到这被认为是不好的,为什么?”。表明你已经在问题上投入了工作,这将大大增加人们愿意为你的问题投入的时间和精力。
在 WinForms 或 WPF 中读取文件没有区别。只需添加到您的窗口 TextBox 并通过 System.IO.File 类加载文件。
例子 :
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<TextBox x:Name="tb" Margin="4"/>
<Button Grid.Row="1" Content="Load file" x:Name="btnLoad" Click="btnLoad_Click" Width="60" HorizontalAlignment="Left" Margin="4"/>
</Grid>
和代码行为:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void btnLoad_Click(object sender, RoutedEventArgs e)
{
tb.Text = File.ReadAllText(@"*path to file*");
}
}