7
Answers

How to use configuration files in C# class library?

Ken H

Ken H

10y
1.2k
1
Hi all,
     I tried to set the initial value of a static field,But it does not  pass compile.
How can I do this: initial value from Web.config or App.config.

 My codes look like this:
Client:
using System;
using System.Text;
using Test;  
namespace DEMO
{

class Client

{

static void Main(string[] args)

{

COMMON.Fun();

Console.ReadLine();

}

}

}
Class library:
using System;
using System.Text;
namespace Test
{


public static class COMMON // It will generate the DLL.


{
     /*

Attempt to set the initial value of a static field.


Seek viable options?
     */
private static readonly string ConnString = System.Web.Configuration.WebConfigurationManager.
ConnectionStrings["SQLConnectionString"].ConnectionString;



static COMMON(){}  // Static constructors
public static void Fun() {
  Console.WriteLine("Connection String is:{0}",ConnString);
    /*
     TO DO SOMETHING ...
    */ 


}


}


}
 
Thanks. 
 
Answers (7)