Settings.Default<性>始终在持久性存储(XML文件)中返回默认值而不是值 [英] Settings.Default.<property> always returns default value instead of value in persistant storage (XML file)

查看:150
本文介绍了Settings.Default<性>始终在持久性存储(XML文件)中返回默认值而不是值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近在C#(.Net 2.0)中写了一个DLL,它包含一个需要IP地址的类。我的同事改变了类从.dll.config(XML)文件中检索IP - 这显然是由他创建的应用程序设置文件自动生成的(Settings1.settings)。这样做的好处是允许最终用户随意更改XML / config文件中的IP地址。



不幸的是,当我将他的代码从树,并尝试编译(或使用)这个新代码,任何调用此DLL的应用程序只获取默认值,而不是文件中的值。



构造函数调用配置文件如下所示:

  public class form:System.Windows.Forms.Form 
{
public form()
{
//这个调用是Windows窗体设计器所必需的。
InitializeComponent();
IP = IPAddress.Parse(Settings1.Default.IPAddress);
}
}

我发现 MSDN论坛上的这个问题的引用,其中用户说:


旧值(您在开发时定义的)值是硬编码的。如果这些法术不能访问或打开配置文件,则会使用默认值。如果您使用dll中的设置,这将始终发生。





  1. 这是否意味着我无法在配置文件中存储DLL的外部值? (我的同事以某种方式做了这个工作...)


  2. 由于我的框架似乎无法访问或打开配置文件,我如何弄清楚为什么会失败?甚至可以检测出这种情况?


Decker :这有点帮助。不幸的是,我正在将这个DLL写入规范,所以我实际上没有访问应用程序的配置文件。如上所述,我的同事创建了一个设置 1 .settings文件。我当时不明白这一点,但现在似乎添加1可以将它从任何调用它的应用程序的设置空间中删除。



我想我想知道的是为什么DLL似乎没有找到位于同一目录旁边的配置文件。另外,我可以将我的程序集的输出类型从类库更改为Windows,因此,逐步跟踪代码一步一步地显示无。



应用程序,并在我的DLL代码开头添加以下行:

  [STAThread] 
public static void Main (string [] args)
{
System.Windows.Forms.Application.Run(new form());
}

当我运行这个,它会生成一个不同的配置文件(.exe .config),我可以改变它,并从文件中拉出新的数据。所以我有点困惑。任何想法?

解决方案

我在解决这个确切的问题在一个应用程序,我正在原型。虽然Decker建议将配置文件一起入侵,但我认为这是一个非常不方便的手动操作,作为构建周期的一部分。而不是我决定最干净的解决方案是让每个库解析自己的library.dll.config文件。它仍然不完美,它需要一些额外的锅炉代码,但它似乎是唯一的办法绕过拜占庭的方式.Net处理这些app.config文件。


I recently wrote a DLL in C# (.Net 2.0) which contains a class that requires an IP address. A co-worker of mine altered the class to retrieve the IP from a ".dll.config" (XML) file -- This apparently is automatically generated by the "Application Settings" file he created (Settings1.settings). The benefit of this was to allow the end-user to change the IP address in the XML/config file at will.

Unfortunately, when I check his code out of the tree and try to compile (or use) this new code, any application calling this DLL only gets the default value, rather than the value from the file.

The constructor that calls the config file looks like this:

    public class form : System.Windows.Forms.Form
    {
        public form()
        {
            // This call is required by the Windows Form Designer.
            InitializeComponent();
            IP = IPAddress.Parse(Settings1.Default.IPAddress);
        }
    }

I found a reference to this problem on the MSDN forums where a user said:

the 'old' values (the ones you define at development time) are hard coded. If the franework isn't able to access or open the config file it will use the defaults instead. This will always happen if you use settings in a dll.

  1. Does this mean that I cannot store an external value for a DLL in a config file? (My co-worker has somehow made this work...)

  2. Since my framework appears to be unable to access or open the config file, how do I figure out why it's failing? Or even detect when this happens?

Decker: That helps a bit. Unfortunately, I am writing this DLL to a specification, so I don't actually have access to the Application's config file. As you'll note above, my co-worker created a "Settings1.settings" file. I didn't understand this at the time, but it seems now that adding the "1" keeps it out of the settings space of any application that calls it.

I guess what I'm trying to figure out is why the DLL doesn't seem to find the config file sitting next to it in the same directory. Tracing thru the code step-by-step reveals nothing.

As an aside, I can change the "Output Type" of my assembly from "Class Library" to "Windows Application" and add the following lines at the beginning of my DLL code:

    [STAThread]
    public static void Main(string[] args)
    {
        System.Windows.Forms.Application.Run(new form());
    }

When I run this, it generates a different config file (a ".exe.config") and that one I can alter and have it pull the new data from the file. So I'm a bit confused. Any ideas?

解决方案

I'm addressing this exact issue in an application I'm in the midst of prototyping. Although Decker's suggestion of hacking the config files together should work I think this is a pretty inconvenient manual hack to perform as part of a build cycle. Instead of that I've decided that the cleanest solution is to just have each library parse its own library.dll.config file. Its still not perfect and it requires some extra boiler-plate code, but it seems to be the only way to get around the byzantine way that .Net handles these app.config files.

这篇关于Settings.Default<性>始终在持久性存储(XML文件)中返回默认值而不是值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆