相关疑难解决方法(0)

相当于库的"app.config"(DLL)

是否有相当于app.config库(DLL)?如果不是,存储特定于库的配置设置的最简单方法是什么?请考虑该库可能在​​不同的应用程序中使用.

.net c# dll settings app-config

144
推荐指数
5
解决办法
15万
查看次数

从同一解决方案C#中的另一个项目引用Web.Config文件

我有一个VC2010 C#解决方案,里面有很多项目.

例如,我有一个Web项目,我有一个类库.

在web.config文件中,我在该<appSettings>部分中有一个键,例如

<add key="FileDirectory" value="G:\ftproot\sales" />
Run Code Online (Sandbox Code Playgroud)

我还在Web.Production.config文件中添加了一个键,以反映服务器上的文件目录.

所以当我在我的web项目中引用它时(它是MVC) - 我这样做:

var FTPPath = ConfigurationManager.AppSettings["FileDirectory"];
Run Code Online (Sandbox Code Playgroud)

这在我的web项目中工作正常.但是,我还需要在类库中引用它,这就是我的问题 - 有没有办法在同一个解决方案中从另一个项目(例如类库)引用web.config文件中的键?

所有帮助表示赞赏.谢谢

c# asp.net projects-and-solutions web-config

13
推荐指数
1
解决办法
1万
查看次数

如何在dll级别阅读app.config.

我在一个解决方案中有一个控制台应用程序项目和库项目(dll).

库项目已在那里我存储我在图书馆使用的一些键值对app.config文件.控制台应用程序引用此dll.

我在控制台应用程序中有另一个app.config文件,我有控制台应用程序的设置.

当我运行程序时,我的library.dll总是从控制台应用程序中引用app.config.

我想分开每个dll的责任,所以我想要一个dll总是引用它自己的 app.config文件而不是调用应用程序的文件.

在这种情况下,library.dll应该使用自己的 app.config ,而不是调用应用程序.

我怎么做到这一点?

c# dll configuration

13
推荐指数
1
解决办法
2万
查看次数

将配置信息放在DLL中

在我的项目中,我有一些功能,可用作Web应用程序和控制台应用程序(从任务调度程序启动).为此,我将公共代码放在Web应用程序和控制台应用程序正在使用的DLL中.这很好用.

但是,控制台和Web应用程序现在具有大致相同的App.config和Web.config.是否可以将此配置放在DLL中并使其可用于两个应用程序?

.net dll configuration

9
推荐指数
1
解决办法
1万
查看次数

如何动态加载单独的应用程序设置文件并与当前设置合并?

有关从单独的配置文件中读取设置的问题以及与其类似的其他配置文件,但我的问题是特定于应用程序属性设置(即<MyApplication.Properties.Settings>- 请参阅下面的XML文件)以及如何动态加载它们.我在这篇文章中尝试了这个方法,其中包括刷新主配置文件的整个appSettings部分,但我的改编引发了异常,因为我没有替换appSettings部分:

var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
// Have tried the other ConfigurationUserLevels to no avail
config.AppSettings.File = myRuntimeConfigFilePath;
config.Save(ConfigurationSaveMode.Modified); // throws ConfigurationErrorsException
ConfigurationManager.RefreshSection("userSettings");
Run Code Online (Sandbox Code Playgroud)

ConfigurationErrorsException.Message是"根元素必须与引用文件的部分的名称匹配'appSettings'(C:\ myFile.xml第2行)." 该文件是:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <userSettings>
        <MyApplication.Properties.Settings>
            <setting name="SineWaveFrequency" serializeAs="String">
                <value>6</value>
            </setting>
            <setting name="SineWaveAmplitude" serializeAs="String">
                <value>6</value>
            </setting>
        </MyApplication.Properties.Settings>
    </userSettings>
</configuration>
Run Code Online (Sandbox Code Playgroud)

有没有办法将此文件中的值导入到MyApplication.Properties.Settings.Default类中,框架处理所有XML反序列化,就像在应用程序启动时加载配置文件一样?

.net settings persistence configuration-files

9
推荐指数
1
解决办法
2万
查看次数

app.config dependentAssembly不工作

我想直接链接到编译/运行时使用的Dll.我的程序布局是这样的:Console Exe启动一个winform dll.该DLL使用一堆dll来执行.Appconfig位于winform dll的项目中.基于一些阅读,winform dll是否正在寻找错误的app.config?我打算使用Assembly.LoadFrom()执行我的dll;

我创建了一个app.config文件,并在该部分中添加了以下行

<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <dependentAssembly>
            <assemblyIdentity name="CommonConversions"
                              publicKeyToken="null"
                              culture="neutral" />
            <codeBase version="1.0.0.0"
                      href="file://C://BMS_ACTD//bin//DX//Globals//CommonConversions.dll"/>
        </dependentAssembly>
        <dependentAssembly>
            <assemblyIdentity name="GlobalConstants"
                              publicKeyToken="null"
                              culture="neutral" />
            <codeBase version="1.0.0.0"
                      href="file://C://BMS_ACTD//bin//DX//Globals//GlobalConstants.dll"/>
        </dependentAssembly>
        <dependentAssembly>
            <assemblyIdentity name="MessageInstance"
                              publicKeyToken="null"
                              culture="neutral" />
            <codeBase version="1.0.0.0"
                      href="file://C://BMS_ACTD//bin//DX//Globals//MessageInstance.dll"/>
        </dependentAssembly>
        <dependentAssembly>
            <assemblyIdentity name="MessageInterface"
                              publicKeyToken="null"
                              culture="neutral" />
            <codeBase version="1.0.0.0"
                      href="file://C://BMS_ACTD//bin//DX//Globals//MessageInterface.dll"/>
        </dependentAssembly>
        <dependentAssembly>
            <assemblyIdentity name="ToolsInterface"
                              publicKeyToken="null"
                              culture="neutral" />
            <codeBase version="1.0.0.0"
                      href="file://C://BMS_ACTD//bin//DX//Globals//ToolsInterface.dll"/>
        </dependentAssembly>
    </assemblyBinding>
</runtime>
Run Code Online (Sandbox Code Playgroud)

位置绝对正确.dll没有强名称,因此publicKeyToken ="null".我的所有版本都是1.0.0.0.当我查看我引用的dll的属性时,文化是空白的.我也应该这样吗?有什么看起来我做错了吗?

c# app-config

8
推荐指数
1
解决办法
9899
查看次数

无需重新编译即可更改.NET应用程序设置

我有一个引用另一个程序集(类库)的Windows服务.在这个其他程序集中,我使用Application Settings来存储一些值.当我将所有必需的文件复制到服务器时,我可以在AssemblyName.dll.config文件中看到这些设置.

但是,当我更改此配置文件中的设置并重新启动服务时,更改无效.即使我卸载/重新安装该服务,它仍然会在运行时返回旧值.

配置文件:

   <setting name="RecordLimit" serializeAs="String">
     <value>300</value>
   </setting>
Run Code Online (Sandbox Code Playgroud)

码:

if (recordCount > Settings.Default.RecordLimit) //always 300
Run Code Online (Sandbox Code Playgroud)

因此,即使我将配置文件中的值更改为400并重新启动甚至重新安装服务,该值始终为300,这使我认为此值存储在已编译的代码中并从其中返回.

我做错了什么,应用程序设置的更改总是需要重新编译并重新安装(我知道我可以使用Save()方法从代码更改设置但这是一个Windows服务,所以使用此方法似乎没有感)?

如果存储在配置文件中的这些设置无效并且设置存储在已编译的代码中,我可以安全地删除这些配置文件吗?

如果没有重新编译我必须存储的其他选项设置,我可以更改这些设置,而无需重新编译?

编辑:我刚刚删除了AssemblyName.dll.config文件,代码返回300,所以配置文件显然是无关紧要的.

.net application-settings

6
推荐指数
1
解决办法
2618
查看次数

是否有Visual Studio加载项的配置类型文件?

创建Visual Studio加载项时,如何使用app.config加载项.如果我将一个添加到项目并进行部署,那么当加载项运行时,我以编程方式尝试通过ConfigurationManager.AppSettings访问它,它不会获取加载项的配置文件.
我是做错了还是有另外一种方法来访问加载项的基于文件的配置设置?

c# configuration .net-4.0 visual-studio-2010 vs-extensibility

5
推荐指数
1
解决办法
2828
查看次数

如何读取任意.config文件而不是app.config

可能重复:
使用ConfigurationManager从任意位置加载配置

我花了几个小时的时间偷偷看看C#框架类来解决这个问题.关于如何做到这一点有几个问题,但没有明确的答案.我能找到的最好的链接是C#DLL配置文件,它完全涵盖了我的用例,从全局.config文件读取.dll,无论哪个进程托管它.

您可以使用反射来创建处理程序对象,但它会降低可读性,这不是生产代码,只是一个片段来显示它的工作原理.我保持尽可能简单明了.

public class ConfigurationManager
{
    public static object GetSection(string sectionName)
    {
        var configuration =  System.Configuration.ConfigurationManager.OpenMappedMachineConfiguration(
                             new System.Configuration.ConfigurationFileMap(
                             @"X:\path\to\your\file.config"));

        object retVal;

        var section = configuration.GetSection(sectionName);
        var xmlDoc = new System.Xml.XmlDocument();
        xmlDoc.LoadXml(section.SectionInformation.GetRawXml());

        if (section.SectionInformation.Type.Equals("System.Configuration.NameValueSectionHandler"))
        {
            var handler = new System.Configuration.NameValueSectionHandler();
            retVal = handler.Create(null, null, xmlDoc.DocumentElement);
        }
        else if (section.SectionInformation.Type.Equals("System.Configuration.DictionarySectionHandler"))
        {
            var handler = new System.Configuration.DictionarySectionHandler();
            retVal = handler.Create(null, null, xmlDoc.DocumentElement);
        }
        else
        {
            throw new System.Exception("Unknown type " + section.SectionInformation.Type);
        }

        return retVal;
    }
}
Run Code Online (Sandbox Code Playgroud)

c# app-config

5
推荐指数
0
解决办法
3253
查看次数

将dll的配置文件移动到调用dll的应用程序

我有一个具有搜索功能的网络应用程序.搜索算法被编译为单独的dll.在搜索算法的C#代码中,我使用设置文件中保存的字符串指向搜索索引所在的目录.编译完搜索代码后,将包含的设置信息Search.dll.config与Search.dll一起放入bin目录中.现在在我的Web应用程序中,我将Search.dll添加到引用.配置文件未添加到Web应用程序中.然而,Web应用程序运行正常,并知道文件的位置.因为如果配置文件不存在,Settings.Designer它内部使用它DefaultSettingValueAttribute来分配默认值.

如何添加Search.dll.config到我的Web应用程序,以便操作员可以根据需要更改服务器上索引文件的位置?

谢谢

编辑:

我尝试将配置文件添加到我的部署文件夹中.但ASP.NET将dll放在C:\ Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root ......目录中,并且配置文件不会复制到那里.所以在这一点上我不知道如何用我的代码包含配置文件.

谢谢你的帮助.

注意:

我一直在使用以下代码将配置文件的值导入应用程序.但是,它取决于dll和配置文件在同一个文件夹中,我不知道如何完成.

    var executingAssembly = System.Reflection.Assembly.GetExecutingAssembly();
    var location = executingAssembly.Location; //C:\MyApp\bin\Debug\Search.dll
    var config = ConfigurationManager.OpenExeConfiguration(location);
    var sections = config.Sections; //count of this is 21
    ConfigurationSectionGroup csg = config.GetSectionGroup("applicationSettings");
    ConfigurationSectionCollection csc = csg.Sections;
    ConfigurationSection cs = csc.Get("Search.Properties.Settings");
Run Code Online (Sandbox Code Playgroud)

deployment dll settings app-config web-config

5
推荐指数
2
解决办法
7412
查看次数