类项目中的配置文件app.config在打包安装后的信息获取的问题
来源:互联网 发布:eviews软件免费下载 编辑:程序博客网 时间:2024/05/18 13:25
在一个项目中碰到这样的一个问题,做一个WORD插件,功能在类库项目中实现了,配置信息存在类库项目的配置文件app.config中,在进行打包后,获取的配置文件中的DocType节点信息时,使用以下方法 ConfigurationManager.AppSettings["DocType"]获取的值总是获取不到,跟踪调试发现值为null,上网看到类库被应用以后,ConfigurationManager.AppSettings访问的是应用程序的配置文件而不是类库所用的配置文件了,所以只有改变策略,把app.config文件也打包到安装文件中,安装后,在安装路径中找到这个文件并进行读取相应的信息,具体的实现方法如下
(.net环境下的C#代码)
//获取文件安装的路径
Assembly SampleAssembly = Assembly.GetExecutingAssembly();
string FilePath = Path.GetDirectoryName(SampleAssembly.CodeBase.Substring(8)) + "//App.config";
//解析配置文件获取对应的值
XmlDocument xDoc = new XmlDocument();
xDoc.Load(FilePath);
string DocType = xDoc.SelectSingleNode(@"/configuration/appSettings/add[@key='DocType']").Attributes["value"].Value;
DocType 就是要获取的值了。
一种强行指定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.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.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文件):
<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被争取读取了:)
- 类项目中的配置文件app.config在打包安装后的信息获取的问题
- 类项目中的配置文件app.config在打包安装后的信息获取的问题
- 类项目中的配置文件app.config在打包安装后的信息获取的问题
- C# 获取app.config中的信息
- Maven打包后配置文件乱码的问题
- 关于C#中的配置文件App.config的读取与更改
- 修改App.Config配置文件可能遇到的问题
- 应用程序配置文件简单问题:winform的配置文件App.config的问题,真的简单?
- java应用程序中处理配置文件和图片路径在打包后路径不对的问题
- Strut的web项目中的配置文件struts-config.mex用处
- 在外围获取APP的机密信息
- Android 获取用户安装的app信息
- Android 获取用户安装的app信息
- 获取未安装的App信息
- 获取已安装APP的信息
- app.config项目配置文件读写
- app.config项目配置文件读写
- app打包安装后第一次启动Home时重复进入启动页的问题
- 广告模式解释
- 如何在同一台服务器上绑定多个域名?
- Weblogic集群概念和配置(二)
- gtk_widget_modify_bg的用法
- dos命令大全
- 类项目中的配置文件app.config在打包安装后的信息获取的问题
- OSD的主要实现方法和类型
- 基于camshift的运动物体跟踪分析
- 特殊字符对应的HTML编码
- oracle dbms_lob.instr
- java 调用
- Weblogic集群概念和配置(五)
- 机房收费系统的UML建模设计
- Weblogic集群概念和配置(四)