gpt4 book ai didi

c# - 如何使用 Visual C# 访问 Visual Studio 中的嵌入式资源?

转载 作者:行者123 更新时间:2023-11-30 18:24:14 26 4
gpt4 key购买 nike

我看过很多关于此的其他帖子,但它们毫无意义。这是我从另一个帖子看到的结构。

var assembly = Assembly.GetExecutingAssembly();
var resourceName = "MyCompany.MyProduct.MyFile.txt";

using (Stream stream = assembly.GetManifestResourceStream(resourceName))
using (StreamReader reader = new StreamReader(stream))
{
string result = reader.ReadToEnd();
}

我不明白 "MyCompany.MyProduct.MyFile.txt" 部分的内容。是“WindowsFormApplication2.Properties.Resources.LabelData.txt”吗?因为那是行不通的。我不断收到 NullReferenceException...我假设它没有正确的文件。这是我现有的代码。

public void readAllLabelValues()
{

var assembly = Assembly.GetExecutingAssembly();
var resourceName = "WindowsFormsApplication2.WindowsFormsApplication2.LabelData.txt";

using (Stream stream = assembly.GetManifestResourceStream(resourceName))

using (StreamReader Reader = new StreamReader(stream))
{
foreach (Label label in tableLayoutPanel1.Controls.OfType<Label>())
{
label.Text = (Reader.ReadLine());
}
}
}

感谢您的帮助。

最佳答案

您可以在微软网站 ( https://support.microsoft.com/en-us/kb/319292 ) 中看到命名约定是:

The compiler adds the root namespace of the project to the name of the resource when it is included in the project. For example, if the root namespace of your project is MyNamespace, the resources are named MyNamespace.MyTextFile.txt and MyNamespace.MyImage.bmp.

您可以通过以下代码使用调试器找出资源名称:

thisExe = System.Reflection.Assembly.GetExecutingAssembly();
string [] resources = thisExe.GetManifestResourceNames();

如果您没有在资源中看到它,请确保您已在文本文件的属性中将构建操作更改为嵌入式资源,而不是默认的内容值。

关于c# - 如何使用 Visual C# 访问 Visual Studio 中的嵌入式资源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31531391/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com