在类库中读取配置文件

C#类库本身不能添加配置文件,但我们仍可在类库中添加相关配置文件读取代码,若其他项目引用了该类库,将会读取到该项目配置文件的相关内容。

示例类库代码:

using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;

namespace TestLib
{
    public class Class1
    {
        public static readonly string ConnectionString = ConfigurationManager.AppSettings["SQLServer"].ToString();
    }
}

若类库被Web应用程序引用,将读取web.config配置文件内容;若被WinForm应用程序引用,将读取app.config配置文件内容。

评论: 0 | 引用: 0 | 查看次数: 4929
发表评论
登录后再发表评论!