C#程序设计实验8 文本文件读写

<Window x:Class="FileReadAndWrite.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:FileReadAndWrite"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <TextBox x:Name="FilePath" VerticalContentAlignment="Center" HorizontalAlignment="Left" Margin="103,26,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="481" Height="40" FontSize="14"/>
        <Button x:Name="FileFind" Content="浏览" HorizontalAlignment="Left" Margin="603,26,0,0" VerticalAlignment="Top" Height="40" Width="70" Click="FileFind_Click"/>
        <TextBlock x:Name="FileName" HorizontalAlignment="Left" Margin="25,36,0,0" TextWrapping="Wrap" Text="文件路径:" VerticalAlignment="Top" Height="36" Width="75" RenderTransformOrigin="-0.151,-0.592" FontSize="14" TextAlignment="Center"/>
        <Button x:Name="FileRead" Content="读取" HorizontalAlignment="Left" Margin="690,26,0,0" VerticalAlignment="Top" Height="40" Width="70" Click="FileRead_Click"/>
        <TextBox x:Name="FileBody" AcceptsReturn="True" TextWrapping="Wrap" Margin="22,77,22,72"/>
        <Button x:Name="FileSave" Content="保存文件" HorizontalAlignment="Center" Margin="0,354,0,0" VerticalAlignment="Top" Height="40" Width="130" Click="FileSave_Click"/>

    </Grid>
</Window>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using Microsoft.Win32;
namespace FileReadAndWrite
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            FilePath.Text="";
            FileBody.Text = "";
            
        }

        private void FileFind_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog f = new OpenFileDialog();
            if (f.ShowDialog() == true)
            {
                FilePath.Text = f.FileName;
            }
        }

        private void FileRead_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //读取文件文本
                //Encoding.Default自动检测编码方式,但中文乱码。。
                //截取文件路径后缀
                string fileAfter = FilePath.Text.Substring(FilePath.Text.Length-3,3);
                //判断是否为文本类型
                if (!fileAfter.Equals("txt"))
                {
                    //如果非文本文件抛出异常
                    throw new Exception("");
                };
                string body = File.ReadAllText(FilePath.Text,Encoding.UTF8);
                //连接指定数组的元素或集合的成员,在每个元素或成员之间使用指定的分隔符
                FileBody.Text = string.Join("\r\n",body);
            }catch(Exception err1)
            {
                MessageBox.Show(err1.Message+"文件非文本文件");
                FileBody = null;
            }
        }

        private void FileSave_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string fileAfter = FilePath.Text.Substring(FilePath.Text.Length-3, 3);
                //判断是否为文本类型
                if (!fileAfter.Equals("txt"))
                {
                    throw new Exception("保存失败!文件非文本文件");
                };
                File.WriteAllText(FilePath.Text, FileBody.Text);
                FileBody.Text = "";
                MessageBox.Show("保存成功!");
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message, "保存失败!");
            }
        }
    }
}


猜你喜欢

转载自blog.csdn.net/qq_62480054/article/details/131585566