在WPF应用程序中提取嵌入资源并转换为XML文件

发布:2024-09-08 16:07 阅读:22 点赞:0

在WPF应用程序中处理嵌入资源时,确保资源正确地包含在程序集并在运行时可以访问是至关重要的。本文将详细介绍如何从嵌入的.txt文件中提取内容,并将其转换为XML文件。

一、添加文本文件到项目中

1.1 添加.txt文件

  1. 在解决方案资源管理器中,右键单击你的项目。
  2. 从下拉菜单中选择“添加” > “现有项”。
  3. 选择你要嵌入的.txt文件并添加到项目中。

1.2 设置文件为嵌入资源

  1. 在解决方案资源管理器中,右键单击刚刚添加的.txt文件,选择“属性”。
  2. 将“生成操作”设置为“嵌入的资源”。

二、读取嵌入资源

2.1 使用 Assembly.GetManifestResourceStream 方法

要读取嵌入资源,你需要使用 Assembly.GetManifestResourceStream 方法,这允许你将嵌入的文件作为流来访问。

using System.IO; // 引入文件操作的命名空间
using System.Reflection; // 引入反射操作的命名空间
using System.Windows; // 引入WPF的命名空间

namespace EmbeddedResourceExample
{
    /// 
    /// MainWindow.xaml 的交互逻辑
    /// 

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent(); // 初始化组件
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var assembly = Assembly.GetExecutingAssembly(); // 获取当前执行的程序集
                string resourceName = "EmbeddedResourceExample.XmlFileStructure.txt"// 嵌入资源的名称
                string filePathToWrite = TxtFilePath.Text; // 从文本框获取要保存文件的路径
                string streamString = null;

                // 获取嵌入资源流
                using (Stream stream = assembly.GetManifestResourceStream(resourceName))
                using (StreamReader reader = new StreamReader(stream))
                {
                    streamString = reader.ReadToEnd(); // 读取流中的所有内容
                }

                // 如果目录不存在,则创建目录
                if (!Directory.Exists(filePathToWrite))
                {
                    Directory.CreateDirectory(filePathToWrite);
                }

                // 将读取的内容保存到指定路径
                File.WriteAllText(Path.Combine(filePathToWrite, "MyTestFile.xml"), streamString);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message); // 异常处理,显示错误信息
            }
        }
    }
}

2.2 XAML视图

<Window x:Class="EmbeddedResourceExample.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:EmbeddedResourceExample"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">


    <Grid>
        <StackPanel Orientation="Vertical">
            <StackPanel Orientation="Horizontal" Margin="10,30,0,0">
                <TextBlock Text="File Path" Width="Auto" Height="26"/>
                <TextBox x:Name="TxtFilePath" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="693" Height="41" Margin="40,0,0,0"/>
            StackPanel>
            <Button Content="Convert txt file to xml" Height="46" Width="725" Click="Button_Click" Margin="10,50,0,0"/>
        StackPanel>
    Grid>
Window>

三、注意事项

  • 确保嵌入的资源名称与代码中的匹配。
  • 路径和文件名应根据实际需求进行调整。
  • 异常处理可以帮助调试和改进应用程序的稳定性。