将数据集存储在 RESX 文件中

发布于 2024-10-27 02:07:19 字数 740 浏览 1 评论 0原文

我对 C# / .Net 文件中的资源非常陌生。

我需要在资源文件 (*.RESX) 中存储操作数据表,但不知道如何操作。

如果我要用 C 进行硬编码,我会这样做:

struct {
   int inputA;
   int outputB;
} dataPoint;

dataPoint myOpsData[] = { { 0,  2000},
                          {10,  4000},
                          {20,  6300},
                          {30,  8400},
                          {40, 10620} };

Visual Studio 2010 编辑器中最简单的方法是使用字符串,但这只将字符串映射到值。如果我这样做,我的数据集将类似于:

Name:       Value:
-----       ------
PerfData1   { 0,  2000}
PerfData2   {10,  4000}
PerfData3   {20,  6300}
PerfData4   {30,  8400}
PerfData5   {40, 10620}

但这种格式需要大量的解析以及相关的验证和错误处理,这似乎是不必要的。

如何在 .RESX 资源文件中表示数据点数组(或更复杂的数据类型)?

I'm very new to resources in C# / .Net files.

I need to store a table of operational data in a resource file (*.RESX), and cannot figure out how to do it.

If I were to do it hardcoded in C, I'd do it like this:

struct {
   int inputA;
   int outputB;
} dataPoint;

dataPoint myOpsData[] = { { 0,  2000},
                          {10,  4000},
                          {20,  6300},
                          {30,  8400},
                          {40, 10620} };

The most straightforward in the Visual Studio 2010 editor is with Strings, but that only maps a string to a value. If I were to do this, my dataset would look something like:

Name:       Value:
-----       ------
PerfData1   { 0,  2000}
PerfData2   {10,  4000}
PerfData3   {20,  6300}
PerfData4   {30,  8400}
PerfData5   {40, 10620}

But this format would require a lot of parsing and associated validation and error handling that just seems unnecessary.

How can I represent arrays of data-points (or even more complex data-types) in a .RESX resource file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

说谎友 2024-11-03 02:07:19

您不必使用 ResX 在程序集中嵌入一些静态数据。

使用 Visual Studio,您只需添加文件(添加新项或添加现有项),将构建操作(F4 或属性窗口)更改为“嵌入式资源”,然后可以使用如下代码加载它(注意:应该在同一个程序集中以避免安全问题):

using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("ConsoleApplication1.MyFile.MyExt"))
{
   // load data from stream
}

我想这里的文件是 MyFile.MyExt,并且该文件直接添加到项目根目录,因为 VS 自动添加当前项目命名空间 - 这里 ConsoleApplication1 - 到嵌入资源路径。

如果您不确定此路径,我建议您使用 .NET Reflector 等工具,该工具能够显示程序集中的嵌入资源名称和数据。

这将为您提供一个 Stream 实例。因此,您可以存储任何类型的数据,只要您可以从 Stream 实例加载/保存数据(MemoryStream、StreamReader、DataSets、.NET 或 Xml 序列化对象、位图等)

You don't have to use a ResX to embed some static data in an assembly.

With Visual Studio, you can just add the file (Add a New Item or Add an Existing Item), change the Build Action (F4 or Properties Window) to "Embedded Resource", and you can then load it with a piece of code like this (NOTE: should be in the same assembly to avoid security issues):

using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("ConsoleApplication1.MyFile.MyExt"))
{
   // load data from stream
}

I suppose here the file is MyFile.MyExt, and this file is directly added to the project root, as VS automatically adds the current project namespace - here ConsoleApplication1 - to embedded resources path.

If you are unsure about this path, I suggest you use a tool such as .NET reflector that is capable of displaying embedded resources names and data from an assembly.

This will get you a Stream instance. So you can store any kind of data provided you can load/save it from/to a Stream instance (MemoryStream, StreamReader, DataSets, .NET or Xml serialized objects, Bitmaps, etc.)

南笙 2024-11-03 02:07:19

使用 app.config 文件很可能会更好。您可以使用 AppSettings 部分添加字符串,显然您需要解析这些字符串(与您尝试对 .RESX 执行的操作类似)。但是,app.config 文件允许您创建可能更适合您的要求的自定义 XML 部分。使用配置文件的最佳部分是能够更改配置数据,而无需实际重建应用程序。

http://www.csharpfriends.com/Articles/getArticle.aspx?articleID= 45

http://www.codeproject.com/KB/aspnet/ConfigSections .aspx

另外,请查看以下线程,但您将拥有数据点而不是页面:

自定义 app.config 配置部分处理程序

You're most likely better off using an app.config file. You can use the AppSettings section to add strings, which obviously you would need to parse (similarly as to what you're trying to do with .RESX). However the app.config file would allow you to create custom XML sections that might fit your requirements better. The best part about using configuration files is being able to change config data without actually having to rebuild the application.

http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=45

http://www.codeproject.com/KB/aspnet/ConfigSections.aspx

Also, take a look at the following thread, but instead of pages you would have data points:

Custom app.config Config Section Handler

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文