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

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

问题描述

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



不幸的是,当我检查他的代码出来的树和尝试编​​译(或使用),这个新的代码,任何应用程序调用此DLL只得到了默认值,而不是从文件中的值。



的构造调用配置文件是这样的:

 公共类形式:System.Windows.Forms.Form中
{
公开形式()
{
//由Windows窗体设计器所必需此调用。
的InitializeComponent();
IP = IPAddress.Parse(Settings1.Default.IPAddress);
}
}



我发现的a参考这个问题在MSDN论坛其中一个用户说:




旧值(你在开发时定义的)是硬编码。如果franework无法访问或打开它会使用默认值,而不是配置文件。如果您在DLL中使用的设置这总会发生。





  1. 这是否意味着我不能存储在配置文件中DLL中的外部价值? (我的同事已经不知何故这项工作......)


  2. 由于我的框架似乎无法访问或打开配置文件,我该怎么办弄清楚它为什么失败?甚至当这种情况发生检测




德克尔:有点帮助。不幸的是,我写这个DLL的规范,所以我实际上并不能够访问应用程序的配置文件。正如您上面的注意,我的同事创造了一个设置 1 .settings文件。我没有时间了解这一点,但现在看来,加入1保持它调用它的任何应用程序的设置空间。



我想我试图找出是什么原因似乎是DLL没有找到配置文件旁边坐在同一个目录中。跟踪直通代码一步一步揭示了什么。



顺便说一句,我可以从类库改变我组装的输出类型为Windows应用程序,并在我的DLL的代码开头添加以下行:

  [STAThread] 
公共静态无效的主要(字串[] args)
{
System.Windows.Forms.Application.Run(新形式());
}

当我运行它,它会产生一个不同的配置文件(一个名为.exe的.config)和一个我可以改变,并将它从文件中拉新的数据。所以我有点糊涂了。任何想法?


解决方案

我解决一个应用程序在我的原型中间是这个确切的问题。虽然黑客配置文件一起德克尔的建议应工作我觉得这是执行作为构建循环的一部分相当不方便手动破解。相反的,我已经决定,最干净的解决方法就是让每个库解析自己的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天全站免登陆