多重專案共用一個app.config 的解決辦法

來源:互聯網
上載者:User

轉自:http://www.cnblogs.com/xiaof2000/archive/2009/12/16/1625198.html

 初用.net 2008,想寫個資料庫設定工作設定資料庫連接,在另一程式中讀取設定資訊。本來簡單的事結果……。

    有人說的.net 的bug,有人用.net 1.0的辦法…… 

    找了許多資料,也不會用settings.settings,但終於簡單的解決了。

    代碼如下:

    兩個程式共用的app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings   >
    <add key ="teststring" value ="testnnnn"/>
  </appSettings>
</configuration>

 

    首先要先在項目中引用System.Configuration 這個檔案,然後再用using System.Configuration 進行引用,不然找不到ConfigurationManager 這個類。

    設定資料庫連接的程式(demo_setup.exe)代碼:

            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            config.AppSettings.Settings["teststring"].Value="ddd";
            config.Save();
    設定資料庫連接的程式(demo_setup.exe)讀取代碼:

           Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
           textBox1.Text = config.AppSettings.Settings["DemoData"].Value.ToString();

---------------------------------

   另一程式(demo.exe)中,讀取非本程式的app.config 的代碼

             Configuration config = ConfigurationManager.OpenExeConfiguration(@"C:\demo_seup.exe");
            textBox1.Text = config.AppSettings.Settings["DemoData"].Value.ToString();

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.