我的 .NET 应用程序何时/如何使用其 App.Config 文件?

发布于 2024-12-23 08:45:05 字数 190 浏览 0 评论 0原文

我以前从来没有想过这个问题;但我最近了解了如何修改 app.config 文件以添加/删除跟踪侦听器(例如,将所有 Trace.WriteLine 输出重定向到文本文件)。

但我不太明白它是如何工作的?有人可以解释一下吗?

我知道相应的 C# 代码与配置(在本示例中)执行相同的操作 - 该代码是否在我的应用程序入口点之前生成/执行?

I've never thought about it before; but I recently learned how I could modify the app.config file to add/remove trace listeners (for example, to redirect all of the Trace.WriteLine output to a text file).

But I don't quite understand how it works? Can someone explain a bit?

I know the corresponding C# code to do the same as the config (in this example) - does that code get generated/executed before my application's entry point?

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

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

发布评论

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

评论(3

尐偏执 2024-12-30 08:45:05

该代码是否在我的应用程序入口点之前生成/执行?

将配置文件视为标准文本文件。如果您的应用程序代码不读取它并对其执行任何操作,则什么也不会发生。因此,基本上,当您在 app.config 文件中定义某些部分时,您的应用程序中(无论是在 BCL 中还是自定义中)都会有一些代码,它们在某个时刻会读取、解析和解释这些值。

那么,让我们考虑一下跟踪侦听器的示例。当您尝试跟踪代码中的某些内容时,底层 Trace 类将使用配置系统来检查您在 app.config 中定义的值。该配置系统仅解析 XML 一次,并将其作为单例存储在内存中,以避免每次的开销。因此,这只是您第一次尝试跟踪配置文件被解析的内容,并且在后续调用中直接从内存中读取值。

does that code get generated/executed before my application's entry point?

Think of a config file just as a standard text file. If your application code doesn't read and do anything with it, nothing will happen. So basically when you define some section in the app.config file, there is some code in your application (either in the BCL or custom) that at some moment will read, parse and interpret the values.

So, let's consider the example of trace listeners. When you try to trace something in your code, the underlying Trace class will use the config system to check the values you have defined in app.config. This config system parses the XML only once and stores it as singleton in memory to avoid the overhead everytime. So, it's only the first time you try to trace something that the config file is parsed and on subsequent calls the values are directly read from memory.

<逆流佳人身旁 2024-12-30 08:45:05

每次启动应用程序时,应用程序都会查看 app.config 文件。

您可以将任何设置存储在 app.config 文件中,并动态添加或删除。

在这里,您可以...

<appSettings>
    <add key="HospitalName" value="HML Hospital" />
    <add key="HospitalAddress" value="Madurai" />
    <add key="ServerName" value="SMSERVER" />
    <add key="DatabaseName" value="HospiCare" />
    <add key="DBUserID" value="sa" />    
    <add key="Theme" value ="Blue"/>
</appSettings>

然后您可以

使用名称空间

using System.Configuration;

将您的配置文件读取为

string theme=ConfigurationManager.AppSettings("Theme");

并使用此修改,如下所示

Configuration configFile = ConfigurationManager.OpenExeConfiguration(System.IO.Path.GetFileName(Application.ExecutablePath));
configFile.AppSettings.Settings(KeyName).Value = KeyValue;
configFile.Save();

接受并投票,如果您找到您的答案

The app.config filed is looked by your application every time you launch it.

You can store any of your settings in your app.config files, add or remove dynamically.

Here you go...

<appSettings>
    <add key="HospitalName" value="HML Hospital" />
    <add key="HospitalAddress" value="Madurai" />
    <add key="ServerName" value="SMSERVER" />
    <add key="DatabaseName" value="HospiCare" />
    <add key="DBUserID" value="sa" />    
    <add key="Theme" value ="Blue"/>
</appSettings>

Then you can alter the same as follows

using the namespace

using System.Configuration;

Read your config file as

string theme=ConfigurationManager.AppSettings("Theme");

and modify by using this

Configuration configFile = ConfigurationManager.OpenExeConfiguration(System.IO.Path.GetFileName(Application.ExecutablePath));
configFile.AppSettings.Settings(KeyName).Value = KeyValue;
configFile.Save();

Accept and vote up if you find your anser

别靠近我心 2024-12-30 08:45:05

正如其他人已经解释的那样。我希望能更深入地解释一下。

在 .NET 中,您的 CLR 为您的应用程序创建虚拟边界。换句话说,当您的应用程序托管在 CLR 中时,它会创建一个虚拟维度供您的应用程序使用。它称为AppDomain或应用程序域。
这是应用程序读取 App.Config 文件以获取任何信息的阶段。因为有时你的 App.Config 会告诉 CLR 它应该寻找什么样的运行时程序集,例如:CLR 2.0 程序集 如果你在 VS2010 中为 2.0 构建应用程序,或者首先为 4 创建项目,然后切换到 2.0 目标,通常会出现这种情况。

As already others have explained. I would just like to explain a bit deeper i hope.

In .NET, your CLR creates a virtual boundary for your application. In other words, when your application is get hosted in CLR, it creates a virtual dimension for your application to play around. Its called AppDomain or Application Domain.
This is the stage when your application reads App.Config file for any information. Because at times your App.Config will tell CLR what kinda run time assemblies it should look for ex: CLR 2.0 assemblies This is usually seen if your build apps in VS2010 for 2.0 or first create project for 4 and then change over to 2.0 target.

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