C# WPF 使用 AssemblyGetEntryAssemblyGetManifestResourceStreamMAC 方法获取程序内部名为 MAC 的资源文件流
要使用 Assembly.GetEntryAssembly().GetManifestResourceStream("MAC") 方法获取名为 MAC 的资源文件流,您需要遵循以下步骤:
-
确保您的资源文件 MAC 已嵌入到您的项目中。在 Visual Studio 中,选择资源文件(一般是一个
.resx文件),然后在属性窗口中将“生成操作”属性设置为“嵌入的资源”。 -
在您的代码中,使用
Assembly.GetEntryAssembly().GetManifestResourceStream("MAC")来获取资源文件流。这里的 "MAC" 是资源的名称,它通常是资源文件的文件名(不包括扩展名)。
下面是一个示例代码,演示了如何获取名为 "MAC" 的资源文件流:
using System;
using System.IO;
using System.Reflection;
using System.Windows;
namespace WpfApp
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
// 获取资源文件流
Stream resourceStream = Assembly.GetEntryAssembly().GetManifestResourceStream("WpfApp.MAC");
if (resourceStream != null)
{
// 在这里处理资源文件流
// 例如,将其保存到本地文件
using (FileStream fileStream = new FileStream("MAC.txt", FileMode.Create))
{
resourceStream.CopyTo(fileStream);
}
}
}
}
}
在上面的示例中,我们将资源文件流保存到了名为 "MAC.txt" 的本地文件中。您可以根据需要对资源文件流进行进一步的处理。请确保将 WpfApp 替换为您的命名空间,以及将 "MAC.txt" 替换为您想要保存资源文件流的文件路径
原文地址: https://www.cveoy.top/t/topic/hJYt 著作权归作者所有。请勿转载和采集!