如何使用 Visual C# 访问 Visual Studios 中的嵌入式资源?
How do I access embedded resources in Visual Studios using Visual C#?
我看过很多关于此的其他 post,但它们毫无意义。这是我从另一个post.
看到的结构
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();
如果您没有在资源中看到它,请确保您已在文本文件的属性中将构建操作更改为嵌入式资源,而不是默认的内容值。
WindowsFormApplication2.LabelData.txt
我发现您可以为此使用 Application.StartUpPath。这是我用来创建有效路径的代码:
string fileLocation = Path.Combine(Application.StartupPath, "programData.txt");
if (!File.Exists(fileLocation))
{
File.Create(fileLocation);
}
然后要对其进行读写,只需创建指向 fileLocation 的 StreamReader 或 StreamWriter。
我看过很多关于此的其他 post,但它们毫无意义。这是我从另一个post.
看到的结构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();
如果您没有在资源中看到它,请确保您已在文本文件的属性中将构建操作更改为嵌入式资源,而不是默认的内容值。
WindowsFormApplication2.LabelData.txt
我发现您可以为此使用 Application.StartUpPath。这是我用来创建有效路径的代码:
string fileLocation = Path.Combine(Application.StartupPath, "programData.txt");
if (!File.Exists(fileLocation))
{
File.Create(fileLocation);
}
然后要对其进行读写,只需创建指向 fileLocation 的 StreamReader 或 StreamWriter。