我知道用c#编写的exe程序可以读取config文件中的配置信息,比如Test.exe,可以在与Test.exe相同目录下放置一个config文件:Test.exe.config,用System.Configuration.ConfigruationSettings可以读取其中的配置信息,但是我现在希望用c#编写一个dll,叫做Log。dll,其主要功能是将我的程序中的错误作为日志记录到一个log文件中,我希望log文件的路径和名称可以动态配置,也就是在一个config文件中配置,但是如何让我的dll读取文件中的配置呢,我试过在dll的同一目录中放置一个Log.dll.config,但是我的dll始终不能将文件中对应的内容读出来,请各位大侠指教,谢谢

--------------------解答-------------------------

一种强行指定dll assembly读取其相应*.dll.config配置文件的方法(又名:如何创建.net 的DCOM)

一般来说,.net 的exe assemly会存在一个对应的*.exe.config配置文件。当需要读取配置信息的时候,可以直接通过ConfigurationManager.AppSettings[index]来读取*.exe.config中的键值,但很少存在dll assembly需要config file的情况。假如当前dll assembly名为test.dll,如果在test.dll中调用ConfigurationManager来读取test.dll.config,那么是无法成功的!

当然,在dll assembly中要读取其*.dll.config这种需求非常少见。但是确要读取的话,可以采取以下方式,即强行制定其dll assembly的路径。

以下代码演示的是:编写一个.net dll并将其发布为一个regasm test.dll /codebase /tlb将其发布为一个DCOM, 然后通过asp页面来调用 。其中在test.dll中的公布的方法HelloWorld()需要读取其test.dll.config中的key value.

dll assembly代码如下:

C# version:

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Configuration;
using System.IO;
using System.Reflection; namespace AnotherDCOM
{
    [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
    public interface ITestClass
    {
        string HelloWorld();
    }     [ClassInterface(ClassInterfaceType.AutoDispatch)]
    public class SimpleClass:ITestClass
    {    
      
        public string HelloWorld()
        {
            Assembly assembly;
            System.Configuration.Configuration configuration;
            ExeConfigurationFileMap map;
         
            Uri uri;
            map = new ExeConfigurationFileMap();
            assembly = Assembly.GetCallingAssembly();
            uri = new Uri(Path.GetDirectoryName(assembly.CodeBase));
            map.ExeConfigFilename = Path.Combine(uri.LocalPath, assembly.GetName().Name + ".dll.config");
            string str=ConfigurationManager.OpenMappedExeConfiguration(map, 0).AppSettings.Settings["MyString"].Value;
            /*
            说明:如果采取如下的传统方法,读取config key
            string str = ConfigurationManager.AppSettings["MyString"];
            如果采取这种方式的话, 则assembly不会读取相应*.dll.config文件;by default,只有exe assembly的配置文件才会被load
             */
            if (str != null)
            {
                return str;
            }
            else
            {
                return "Hello World .net Another DCOM, Can't read config";
            }
       
        }
    }
}

VB.net version

Imports System
Imports System.Runtime.InteropServices
Imports System.Reflection
Imports System.Configuration
Imports System.IO Namespace NetDcom
    <InterfaceType(ComInterfaceType.InterfaceIsIDispatch)> Public Interface ITestClass
        Function HelloWorld() As String
    End Interface     <ClassInterface(ClassInterfaceType.AutoDispatch), ProgId("NetDcom.SimpleClass"), ComVisible(True)> Public Class SimpleClass
        Implements ITestClass
        Public Function HelloWorld() As String Implements ITestClass.HelloWorld
            Dim assembly As Assembly
            Dim configuration As Configuration
            Dim map As ExeConfigurationFileMap
            Dim str As String
            Dim uri As Uri
            map = New ExeConfigurationFileMap
            [assembly] = assembly.GetCallingAssembly
            uri = New Uri(Path.GetDirectoryName([assembly].CodeBase))
            map.ExeConfigFilename = Path.Combine(uri.LocalPath, ([assembly].GetName.Name & ".dll.config"))
            str = ConfigurationManager.OpenMappedExeConfiguration(map, 0).AppSettings.Settings.Item("MyString").Value             If String.IsNullOrEmpty(str) <> True Then
                HelloWorld = str
            Else
                HelloWorld = "Hello World, .net DCOM(vb.net)"
            End If         End Function     End Class
End Namespace

app.config配置文件如下(编译后会自动生成test.dll.config文件):

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="MyString" value="Hello World"/>    
  </appSettings>
</configuration>

编译发布之后,运行以下command将其注册为DCOM

regasm test.dll /codebase /tlb

说明:如果.net dll要注册成为COM的话,需要几个必备的条件:

1. dll要设置相应的System.runtime.InteropServices的相应attribute,如山代码所示。

2. (C#中)assemblyinfo.cs中, comvisible要设置为true。

3. 生成dll assembly的时候,一定要选中 Register for ComInterop复选框,如下所示

然后,通过asp调用该DCOM如下:

<%
set obj=CreateObject("AnotherDCOM.SimpleClass")
Response.write obj.HelloWorld()
%>

通过procmon我们可以看到,该dll.config被争取读取了:)

c# 如何使用DLL的config文件中的信息的更多相关文章

  1. 在Web.Config文件中使用configSource,避免动态修改web.config导致asp.net重启(另添加一个Config文件用于管理用户数据)

    原文:在Web.Config文件中使用configSource,避免动态修改web.config导致asp.net重启(另添加一个Config文件用于管理用户数据) 我们都知道,在asp.net中修改 ...

  2. Web.config 文件中的 system.webServer

    Web.config 文件中的 system.webServer 节用于指定适用于 Web 应用程序的 IIS 7.0 设置.system.WebServer 是 configuration 节的子级 ...

  3. web.config文件中配置数据库连接的两种方式

    web.config文件中配置数据库连接的两种方式 标签: 数据库webconfig 2015-04-28 18:18 31590人阅读 评论(1)收藏举报    分类: 数据库(74)  在网站开发 ...

  4. Machine.config 文件中节点<machineKey>的强随机生成

    Machine.config 文件中节点<machineKey>的强随机生成 <machineKey>这个节允许你设置用于加密数据和创建数字签名的服务器特定的密钥.ASP.NE ...

  5. 如何在web.config文件中配置Session变量的生命周期

    实例说明:在网上购物商城中,为了维护在线购物环境,一般只有注册会员才可以购买商品.实现购物功能时,先通过Session变量记录会员的登录名,然后在购买商品页面通过判断会员是否登录确定其能否购买商品. ...

  6. C# 关于config文件中的usersettings

    在调整app.config的时候遇到了一点问题,把这个问题记录下来,可能我只是没有找到解决方案,问题本身也许并不复杂. 在VS中通过Properties中的Settings.settings来设置作用 ...

  7. jmeter csv Data Set Config 文件中带引号的数据转换问题(自动添加双引号解决办法)

    1.我们从csv中获取数据,在jmeter中使用这些数据,其中csv的数据如图,有的数据包含引号. 2.问题:我们获取的json数据,被自动添加了双引号 3.解决方式: 在CSV Data Set C ...

  8. (转)linux sudo 重定向,实现只有系统管理员才有权限操作的文件中写入信息

    众所周知,使用 echo 并配合命令重定向是实现向文件中写入信息的快捷方式. 本文介绍如何将 echo 命令与 sudo 命令配合使用,实现向那些只有系统管理员才有权限操作的文件中写入信息.   比如 ...

  9. c++------------提取文件中的信息

    对于文件比较复杂的时候,为了获取文件中的信息,需要一些比较特殊的函数,比如,getline().replace().atoi,atof等 例子一,读取以下文件中的数据,并保存进一个类里面. 首先,类的 ...

随机推荐

  1. C#中结构体的声明

    定义:       结构是用户自定义的值类型 代码样式:struct Pair{    public int X, Y; //公有变量名单词的首字母大写(PascalCase规则)}struct Pa ...

  2. HDU 1425 sort 【哈希入门】

    题意:给出n个数,输出前m大的数 和上一题一样,将输入的数加上一个极大地值作为地址 #include<iostream> #include<cstdio> #include&l ...

  3. [反汇编练习] 160个CrackMe之008

    [反汇编练习] 160个CrackMe之008. 本系列文章的目的是从一个没有任何经验的新手的角度(其实就是我自己),一步步尝试将160个CrackMe全部破解,如果可以,通过任何方式写出一个类似于注 ...

  4. apache开源项目 -- tomee

    Apache TomEE,发音是“Tommy”,是一个经Apache.JavaEE6.Web框架认证的适配器,其在Tomcat服务器中是最强大的.Apache TomEE是由香草项目(简化常见编程任务 ...

  5. FFMPEG 库移植到 VC 需要的步骤

    在VC下使用FFMPEG编译好的库,不仅仅是把.h,.lib,.dll拷贝到工程中就行了,还需要做以下几步.(此方法适用于自己使用MinGW编译的库,也同样适用于从网上下载的编译好的库,例如http: ...

  6. HTML5实现扫描识别二维码/生成二维码

    扫描识别二维码 思路: 1. 操作摄像头,获取图片.HTML5 WEBRTC的navigator.getUserMedia方法去实时获取摄像头资源.  2. 利用canvas使用相关算法分析图片识别图 ...

  7. 【进阶——种类并查集】hdu 1829 A Bug's Life (基础种类并查集)TUD Programming Contest 2005, Darmstadt, Germany

    先说说种类并查集吧. 种类并查集是并查集的一种.但是,种类并查集中的数据是分若干类的.具体属于哪一类,有多少类,都要视具体情况而定.当然属于哪一类,要再开一个数组来储存.所以,种类并查集一般有两个数组 ...

  8. 火狐和google游览器的 hack独有识别 css

    先来看google的: /* 这针对于webkit内核的游览器.包括苹果谷歌游览器等*/ @media screen and (-webkit-min-device-pixel-ratio:0) { ...

  9. <转>python version 2.7 required,which was not found in the registry

    安装PIL-1.1.7.win32-py2.7的时候,不能再注册表中识别出来python2.7 方法:新建一个register.py 文件,把一下代码贴进去,保存 # # script to regi ...

  10. CentOS7 mariadb 修改编码

    CentOS7 mariadb 编码的修改: 网上看了不少的解决方案,要么是比较老的,要么是不正确,测试成功的方式,记录备查. 登录MySQL,使用SHOW VARIABLES LIKE 'chara ...