You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现文本文件加载及多TextBox信息展示程序(附代码)

简易文本文件加载与显示程序完整实现方案

嘿,我来帮你把这个文本文件加载程序给搞定!根据你的需求,我们要实现选择文本文件后,把文件名、文件路径和内容分别展示在三个独立的TextBox里。下面是完整的代码实现,包含XAML布局和后台逻辑,我会顺便拆解关键细节:

第一步:XAML布局(MainWindow.xaml)

先把界面搭好,三个TextBox对应不同的展示内容,再加一个按钮用来触发文件选择:

<Window x:Class="ListBuilderTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="文本文件查看器" Height="450" Width="800">
    <Grid Margin="10">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
        
        <!-- 文件名展示区 -->
        <Label Grid.Row="0" Grid.Column="0" Content="文件名:" Margin="0,0,5,5"/>
        <TextBox Grid.Row="0" Grid.Column="1" x:Name="txtFileName" IsReadOnly="True" Margin="0,0,0,5"/>
        
        <!-- 文件路径展示区 -->
        <Label Grid.Row="1" Grid.Column="0" Content="文件路径:" Margin="0,0,5,5"/>
        <TextBox Grid.Row="1" Grid.Column="1" x:Name="txtFilePath" IsReadOnly="True" Margin="0,0,0,5"/>
        
        <!-- 文件内容展示区 -->
        <Label Grid.Row="2" Grid.Column="0" Content="文件内容:" Margin="0,0,5,5"/>
        <TextBox Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="2" x:Name="txtFileContent" 
                 IsReadOnly="True" AcceptsReturn="True" VerticalScrollBarVisibility="Auto"/>
        
        <!-- 文件选择按钮 -->
        <Button Grid.Row="0" Grid.Column="2" Content="选择文本文件" 
                Click="BtnSelectFile_Click" Margin="5,0,0,5"/>
    </Grid>
</Window>

第二步:后台逻辑(MainWindow.xaml.cs)

完善你给出的代码,实现文件选择、读取和内容填充的功能:

using System.IO;
using System.Windows;
using System.Windows.Forms; // 注意:需要在项目中引用System.Windows.Forms程序集

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

        private void BtnSelectFile_Click(object sender, RoutedEventArgs e)
        {
            // 创建文件选择对话框,只筛选文本文件
            using (OpenFileDialog openFileDialog = new OpenFileDialog())
            {
                openFileDialog.Filter = "文本文件 (*.txt)|*.txt|所有文件 (*.*)|*.*";
                openFileDialog.FilterIndex = 1; // 默认选中文本文件筛选器
                openFileDialog.RestoreDirectory = true; // 关闭对话框后回到之前的目录

                // 如果用户选择了文件并确认
                if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    string filePath = openFileDialog.FileName;
                    
                    // 提取文件名(带扩展名),如果要无扩展名用Path.GetFileNameWithoutExtension(filePath)
                    string fileName = Path.GetFileName(filePath);
                    // 读取文件内容,中小型文件用ReadAllText足够高效
                    string fileContent = File.ReadAllText(filePath);

                    // 把数据填充到对应的TextBox
                    txtFileName.Text = fileName;
                    txtFilePath.Text = filePath;
                    txtFileContent.Text = fileContent;
                }
            }
        }
    }
}

关键细节说明

  • 程序集引用:因为用到了OpenFileDialog,需要在项目的引用中添加System.Windows.Forms(右键项目→添加→引用→程序集→找到并勾选它)。
  • 大文件处理:如果要支持超大文本文件,建议替换File.ReadAllText为StreamReader逐行读取并追加到txtFileContent,避免内存溢出:
    using (StreamReader reader = new StreamReader(filePath))
    {
        string line;
        while ((line = reader.ReadLine()) != null)
        {
            txtFileContent.AppendText(line + "\n");
        }
    }
    
  • 只读设置:所有TextBox都设为IsReadOnly="True",防止用户误修改展示的内容,同时保留文本选中复制的功能。

内容的提问来源于stack exchange,提问作者Phil Zan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:52:09