我应该如何在 C# 中组织特定于项目的只读文件

发布于 2024-07-29 20:38:25 字数 917 浏览 8 评论 0原文

我的独立小型 C# 项目需要中等数量(大约 100 个)的 (XML) 文件,这些文件需要在运行时提供特定于域的值。 它们不需要对用户可见。 然而,我需要偶尔添加或更新它们,我准备手动执行这些操作(即我不设想特定的工具,特别是因为它们可能是在系统外部创建的)。

我希望它们可以重新定位(即使用相对文件名)。 我应该考虑哪些选项来组织它们以及打开和阅读它们需要哪些调用?

该项目本质上是独立的(与 Web 服务、数据库或其他第三方应用程序无关)。 它被组织成少量的命名空间,并且文件的所有逻辑都可以限制在单个命名空间中。

========= 很抱歉不清楚。 我会再试一次。 在 Java 应用程序中,可以包含相对于类路径而不是最终的 *.exe 读取的资源文件。 我相信在 C# 中有一种方法可以做类似的事情。

========= 我相信我应该使用与 RESX 相关的东西。 请参阅(RESX 文件和 xml 数据 https://stackoverflow.com/posts/1205872/edit)。 我可以将字符串放入 resx 文件中,但这很乏味且容易出错,我更愿意将它们复制到适当的位置。

很抱歉我不清楚,但我不太确定如何提出这个问题。

========= 这个问题似乎非常接近 (C# 相当于 getClassLoader().getResourceAsStream(...) )。 我希望能够在 VisualStudio 中添加文件 - 我的问题是我应该将它们放在哪里以及如何指示它们是资源?

My standalone smallish C# project requires a moderate number (ca 100) of (XML) files which are required to provide domain-specific values at runtime. They are not required to be visible to the users. However I shall need to add to them or update them occasionally which I am prepared to do manually (i.e. I don't envisage a specific tool, especially as they may be created outside the system).

I would wish them to be relocatable (i.e. to use relative filenames). What options should I consider for organizing them and what would be the calls required to open and read them?

The project is essentially standalone (not related to web services, databases, or other third-party applications). It is organised into a small number of namespaces and all the logic for the files can be confined to a single namespace.

=========
I am sorry for being unclear. I will try again. In a Java application it is possible to include resource files which are read relative to the classpath, not to the final *.exe. I believe there is a way of doing a similar thing in C#.

=========
I believe I should be using somthing related to RESX. See (RESX files and xml data https://stackoverflow.com/posts/1205872/edit). I can put strings in a resx files, but this is tedious and error-prone and I would prefer to copy them into the appropriate location.

I am sorry to be unclear, but I am not quite sure how to ask the question.

=========
The question appears to be very close to (C# equivalent of getClassLoader().getResourceAsStream(...)). I would like to be able to add the files in VisualStudio - my question is where do I put them and how do I indicate they are resources?

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

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

发布评论

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

评论(3

弱骨蛰伏 2024-08-05 20:38:25

如果将它们放在与可执行文件相关的子文件夹中,例如 .\Config,您将能够使用 File.ReadAllText(@"Config\filename.xml")

如果您有 ASP.NET 应用程序,则可以将它们放入特殊的 App_Data 文件夹中,并使用 File.ReadAllText(Server.MapPath("~/App_Data/filename.xml")) 访问它们

If you put them in a subfolder relative to your executable, say .\Config you would be able to access them with File.ReadAllText(@"Config\filename.xml").

If you have an ASP.NET application you could put them inside the special App_Data folder and access them with File.ReadAllText(Server.MapPath("~/App_Data/filename.xml"))

冷…雨湿花 2024-08-05 20:38:25

我仍然不清楚您是否有 Win 表单或 Web 项目...无论如何,不​​需要使用相对文件名来使它们可重定位。 我建议在 web.config 或 app.config 文件的 appSettings 部分中添加一个条目,其中包含 xml 文件的完整路径。 如果它们被移动,您所要做的就是更改您的配置文件。

<!-- app/web.config --->
<appSettings>
        <add key="filelocation" value="c:\xmlfiles"/>
</appSettings>

//c# code (in ASP.NET app, for example)
using System.IO;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string path = ConfigurationManager.AppSettings["filelocation"];
        DirectoryInfo di = new DirectoryInfo(path);
        List<XDocument> xmlDocs = new List<XDocument>();
        foreach (FileInfo fi in di.GetFiles())
        {
            xmlDocs.Add(XDocument.Load(fi.FullName));
        }
    }
}

当然,您可能知道也可能不知道 xml 文件名,因此您可能不会以完全相同的方式加载和读取它们。

就您而言,如果您有 ASP.NET 站点(假设它永远不会位于多个服务器上),那么将它们放在 App_Data 文件夹中似乎是完全合适的。 如果您在配置文件中列出“~/App_Data”,请务必小心,您知道要将其转换为完整路径,就像 darin 使用 Server.MapPath() 所做的那样。

I'm still unclear whether you have a Win forms, or web project... Regardless, use of relative file names is not required to have them relocatable. I would suggest adding an entry in your web.config or app.config file's appSettings section containing the full path to your xml files. If they are moved, all you have to do is change your config file.

<!-- app/web.config --->
<appSettings>
        <add key="filelocation" value="c:\xmlfiles"/>
</appSettings>

//c# code (in ASP.NET app, for example)
using System.IO;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string path = ConfigurationManager.AppSettings["filelocation"];
        DirectoryInfo di = new DirectoryInfo(path);
        List<XDocument> xmlDocs = new List<XDocument>();
        foreach (FileInfo fi in di.GetFiles())
        {
            xmlDocs.Add(XDocument.Load(fi.FullName));
        }
    }
}

Of course you may or may not know the xml file names, so you may not load up and read them in quite the same way.

In your case it seems totally appropriate to put them in your App_Data folder if you have a ASP.NET site (assuming it's never going to be on more than one server). Just be careful if you list "~/App_Data" in your config file you know to translate that into a full path like darin did with Server.MapPath().

烟酒忠诚 2024-08-05 20:38:25

http://www.attilan.com/2006/08/ 有详细的答案accessing_embedded_resources_u.php。 看来该文件必须指定为 EmbeddedResource。 我还没有让它发挥作用,但这就是我想要的。

There is a detailed answer on http://www.attilan.com/2006/08/accessing_embedded_resources_u.php. It appears that the file has to be specified as an EmbeddedResource. I have not yet got this to work but it is what I want.

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