開發手記之實現web.config的快速

來源:互聯網
上載者:User
  在Web開發中,對web.cofig進行配置是非技術人員無法勝任的工作,但是常常需要由客戶自己來進行簡單配置的時候,需要提供一個有效工具來指導客戶完成這項操作,並且防止無效或錯誤的更改。
 
 解決方案:
 
  首先,必須瞭解對系統的配置主要包括machine.config和web.config兩個部分,這兩個檔案本質上是Xml檔案,包含了ASP.NET的所有配置資訊。因此,對系統的配置,實際上是對Xml檔案的操作,因此,我們可以採取對Xml檔案的讀寫操作,來實現快速配置的思路。在此我們主要以web.config為例來說明,Web.config中的各個資料項目表示的內容,不是探討的重點,具體內容可以參考Msdn的說明。
 
  實現的核心代碼為:
 
 
  private void btnOK_Click(object sender, System.EventArgs e)
  {
  //定義變數
  string strLocation=txtLocation.Text;
  string strProvider=txtProvider.Text;
  string strMode=txtMode.Text;
  string strUser=txtUser.Text;
  string strDataSource=txtDataSource.Text;
  string strPwd=txtPwd.Text;
 
  string semicolon=";";
 
  //操作XML節點
  XmlDocument xmlDoc=new XmlDocument();
  xmlDoc.Load("myXML.xml");
  XmlNode xNode=xmlDoc.SelectSingleNode("//appSettings/add[@key='oledbConnection1.ConnectionString']");
  if(xNode!=null)
  {
  xNode.Attributes[ "value"] .Value="Location="+strLocation+semicolon+"Provider="+strProvider+semicolon+
  "Mode="+strMode+semicolon+"User ID="+strUser+semicolon+"Data Source="+strDataSource+semicolon+
  "Password="+strPwd;
  }
  xmlDoc.Save("myXML.xml");
 
  MessageBox.Show("設定成功!");
  }
 
 
 
 代碼中,我們以myXML.xml為例,可以代表其他任何XML的修改。
 
 這些只是簡單的一個資料項目的操作,更進一步的操作需要繼續完善。
 
 在下面的操作介面上,非技術人員就可以很方便的修改其中的各項資訊。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.