简体   繁体   English

如何阅读资源txt文件的文本并将其放入C#.net 4.0 WPF中的TextBox中?

[英]How do I read the text of a resource txt file and put it into a TextBox in C# .net 4.0 WPF?

How do I read the text of a resource txt file and put it into a TextBox in C# .net 4.0 WPF? 如何阅读资源txt文件的文本并将其放入C#.net 4.0 WPF中的TextBox中? I have a TextBox in my form and a TXT file in my resources folder, how do I put the text from the TXT file (including the line breaks, double spaces, etc) into the TextBox? 我的表单中有一个TextBox,资源文件夹中有一个TXT文件,如何将TXT文件中的文本(包括换行符,双倍空格等)放入TextBox中?

Please simplify the code for me because I'm a beginner in C#... Thanks! 请简化代码,因为我是C#的初学者。谢谢!

No need to provide code, there is already a very simple WPF Example for this exact problem, found here . 无需提供代码,已经有这种确切的问题,发现了一个很简单的例子WPF 这里

And btw, don't ask on SO for "Hey could you guys solve that for me!" 顺便说一句,不要问SO“你们可以帮我解决这个问题!” you should ask questions more like "Hey i tried this, and that. Why didn't this work, what is wrong with that. I also read somewhere that this is considered bad, why exactly?". 您应该问更多的问题,例如“嘿,我尝试过这个,那。为什么这没用,那是什么问题。我还在某处读到这被认为是不好的,为什么呢?”。 Show that you put work already into the problem, this will greatly increase the amount of time and effort people are willingly to put into your problem. 表明您已经将工作投入到问题中,这将大大增加人们乐于投入到问题中的时间和精力。

No difference reading file in WinForms or in WPF. 在WinForms或WPF中读取文件没有差异。 Simply add to you window TextBox and load file via System.IO.File class. 只需将TextBox添加到您的窗口中,然后通过System.IO.File类加载文件。

Example : 范例:

<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>

and code behavior : 和代码行为:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void btnLoad_Click(object sender, RoutedEventArgs e)
    {
        tb.Text = File.ReadAllText(@"*path to file*");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM