读取Web.config的源码(C#.net 2005)

来源:互联网 发布:mac系统怎么打开终端 编辑:程序博客网 时间:2024/06/04 17:53

1:读取过程

public void ApplyConnectionInfo() {
            string strConnection = string.Empty;           
            doc.Load(this.openFileName);
        
            foreach (XmlNode node in doc["configuration"]["connectionStrings"].ChildNodes) {
                if (node.Name == "add") {
                    if (node.Attributes["name"].Value == "connectionString") {
                        strConnection = node.Attributes["connectionString"].Value;
                    }
                }               
            }
            string[] ConnInfo = strConnection.Split(';');
            for (int i = 0; i < ConnInfo.Length; i++) {
                string strTemp = ConnInfo[i].Trim();
                if (strTemp.Length == 0) continue;
                string Perfix = strTemp.Substring(0, strTemp.IndexOf("="));
                if(Perfix.ToUpper().Equals("SERVER")) this.serverName = strTemp.Substring(strTemp.IndexOf("=") + 1);              
                if (Perfix.ToUpper().Equals("DATABASE")) this.database = strTemp.Substring(strTemp.IndexOf("=") + 1);
                if (Perfix.ToUpper().Equals("UID")) this.userName = strTemp.Substring(strTemp.IndexOf("=") + 1);
                if (Perfix.ToUpper().Equals("PWD")) this.password = strTemp.Substring(strTemp.IndexOf("=") + 1);            
            }

            foreach (XmlNode node in doc["configuration"]["appParams"].ChildNodes) {
                if (node.Name == "add") {
                    if (node.Attributes["key"].Value == "remoteHostString") {
                        this.remoteIP = node.Attributes["value"].Value;
                    }
                }
            }

            foreach (XmlNode node in doc["configuration"]["system.web"].ChildNodes) {
                if (node.Name == "identity") {                   
                     this.SNuserName = node.Attributes["userName"].Value;
                     this.SNpassword = node.Attributes["password"].Value;                   
                }
            }

        } 

2:修改并保存

public void SaveConnectionInfo() {
            try {
                string strConnectionstring = String.Format("Server={0};Database={1};uid={2};pwd={3}", this.ServerName, this.DataBaseName, this.UserID, this.UserPassword);               
                doc.Load(this.OpenPath);
                foreach (XmlNode node in doc["configuration"]["connectionStrings"].ChildNodes) {
                    if (node.Name == "add") {
                        if (node.Attributes["name"].Value == "connectionString") {
                            node.Attributes["connectionString"].Value = strConnectionstring;
                        }
                    }
                }
                foreach (XmlNode node in doc["configuration"]["appParams"].ChildNodes) {
                    if (node.Name == "add") {
                        if (node.Attributes["key"].Value == "remoteHostString") {
                            node.Attributes["value"].Value = this.RemoteIP;
                        }
                        if (node.Attributes["key"].Value == "connectionString") {
                            node.Attributes["value"].Value = strConnectionstring;
                        }
                    }
                }

                foreach (XmlNode node in doc["configuration"]["system.web"].ChildNodes) {
                    if (node.Name == "identity") {
                        node.Attributes["userName"].Value=this.SNuserName ;
                        node.Attributes["password"].Value=this.SNpassword ;
                    }
                }
               
                System.IO.File.SetAttributes(this.openFileName, System.IO.FileAttributes.Normal);

                doc.Save(this.openFileName);
                System.IO.File.SetAttributes(this.openFileName, System.IO.FileAttributes.ReadOnly);
                MessageBox.Show("PMFE web confirg file setting is saved!");
            } catch (Exception e) {
                MessageBox.Show("Error,Save is faile!");
            }
        }

3:'Web.confirg文件

<?xml version="1.0"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
  <configSections>
    <section name="uipConfiguration" type="Microsoft.ApplicationBlocks.UIProcess.UIPConfigHandler,Microsoft.ApplicationBlocks.UIProcess, Version=1.0.1.0,Culture=neutral,PublicKeyToken=null" />
    <section name="appParams" type="System.Configuration.NameValueFileSectionHandler, System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
  </configSections>
  <uipConfiguration enableStateCache="true" allowBackButton="false">
    <objectTypes>
      <iViewManager name="WebFormViewManager" type="Microsoft.ApplicationBlocks.UIProcess.WebFormViewManager, Microsoft.ApplicationBlocks.UIProcess, Version=1.0.1.0,Culture=neutral,PublicKeyToken=null" default="true" />
      <state name="State" type="Microsoft.ApplicationBlocks.UIProcess.State, Microsoft.ApplicationBlocks.UIProcess, Version=1.0.1.0,Culture=neutral,PublicKeyToken=null" default="true" />
      <controller name="WelcomeController" type="PMFE.UI.Component.Common.WelcomeController,PMFE.UI.Component, Version=1.0.0.0,Culture=neutral,PublicKeyToken=null" />
            <statePersistenceProvider name="MemoryPersistState" type="Microsoft.ApplicationBlocks.UIProcess.MemoryStatePersistence, Microsoft.ApplicationBlocks.UIProcess, Version=1.0.1.0,Culture=neutral,PublicKeyToken=null" connectionString="server=.;database=UIPState;uid=rpms;pwd=rpms" default="true" />
    </objectTypes>
    <views>
      <view name="WELCOME" type="Welcome.aspx" controller="WelcomeController" />
      </views>
  </uipConfiguration>
  <system.web>
    <identity impersonate="true" userName="Angel Wong" password="axio55" />
    <compilation defaultLanguage="c#" debug="true">
      <assemblies>
        <add assembly="System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />
        <add assembly="System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
        <add assembly="stdole, Version=7.0.3300.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />
        <add assembly="Infragistics2.WebUI.Shared.v5.3, Version=5.3.20053.73, Culture=neutral, PublicKeyToken=7dd5c3163f2cd0cb" />
               <add assembly="CrystalDecisions.CrystalReports.Engine, Version=10.2.3600.0, Culture=neutral, PublicKeyToken=692FBEA5521E1304" />
        <add assembly="CrystalDecisions.ReportSource, Version=10.2.3600.0, Culture=neutral, PublicKeyToken=692FBEA5521E1304" />
        <add assembly="CrystalDecisions.Shared, Version=10.2.3600.0, Culture=neutral, PublicKeyToken=692FBEA5521E1304" />
        <add assembly="CrystalDecisions.Web, Version=10.2.3600.0, Culture=neutral, PublicKeyToken=692FBEA5521E1304" />
        <add assembly="Infragistics2.WebUI.UltraWebGrid.ExcelExport.v5.3, Version=5.3.20053.73, Culture=neutral, PublicKeyToken=7DD5C3163F2CD0CB" />
        <add assembly="Infragistics2.Excel.v5.3, Version=5.3.20053.73, Culture=neutral, PublicKeyToken=7DD5C3163F2CD0CB" />
      </assemblies>
      <compilers>
        <compiler language="c#" type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" extension=".cs" compilerOptions="/d:DEBUG;TRACE" />
      </compilers>
    </compilation>
    <customErrors mode="RemoteOnly" />
    <authentication mode="None" />
    <!--<identity impersonate="true" userName="*" password="*" />-->
    <!--<authorization>
            <deny users="?" />
        </authorization>-->
    <trace enabled="false" requestLimit="10" pageOutput="true" traceMode="SortByTime" localOnly="true" />
    <sessionState mode="InProc" cookieless="false" timeout="10" />
    <globalization requestEncoding="utf-8" responseEncoding="utf-8" />
    <xhtmlConformance mode="Legacy" />
    <pages />
  </system.web>
  <appParams>
    <add key="connectionString" value="Server=Server06;Database=PMFE;uid=*;pwd=*" />
    <add key="remoteHostString" value="192.168.170.99" />
  </appParams>
  <connectionStrings>
    <add name="connectionString" connectionString="Server=Server06;Database=PMFE;uid=*;pwd=*" providerName="System.Data.SqlClient" />
  </connectionStrings>
  <location path="en">
    <system.web>
      <globalization culture="en" uiCulture="en" />
    </system.web>
  </location>
</configuration>

原创粉丝点击