在 Config 檔中設定一般變數與連線變數
[C#.NET][VB.NET] 應用程式組態設定檔 / Application Configurable File (一)
http://www.dotblogs.com.tw/yc421206/archive/2009/04/14/8000.aspx
[C#.NET][VB.NET] 應用程式組態設定檔 / Application Configurable File (二)
http://www.dotblogs.com.tw/yc421206/archive/2010/08/08/17076.aspx
connectionStrings
保護連接字串: http://msdn.microsoft.com/zh-tw/library/89211k9b(v=vs.80).aspx
<!--連線設定分別需要輸入HOST、PORT、SID、User Id and Password-->
<connectionStrings>
<add name="OracleConnectionStr" connectionString="Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.X.X)(PORT=1521)))
(CONNECT_DATA=(SID=XE)));User Id=system;Password=oracle"/>
</connectionStrings>
</configuration>
using System.Data.OracleClient; //取得"Oracle相關物件"必需的
//取得設定在config檔裡的變數
string connectionStr = ConfigurationManager.ConnectionStrings["OracleConnectionStr"].ToString();
//連接 DB
OracleConnection oConn = new OracleConnection(connectionStr);
…
appSettings
<appSettings>
<add key = "Variable1" value ="abcdefg"/>
<add key = "Variable2" value ="1234567"/>
</appSettings>
</configuration>
//取得設定在config檔裡的變數
string Str1 = ConfigurationManager.AppSettings["Variable1"];
string Str2 = ConfigurationManager.AppSettings["Variable2"];
applicationSettings, userSettings
http://msdn.microsoft.com/zh-tw/library/a65txexh(v=vs.80).aspx
利用VS的UI加入變數, 點選專案右鍵> 屬性 > 設定 (操作步驟: http://msdn.microsoft.com/zh-tw/library/25zf0ze8(v=vs.90).aspx)
<WindowsFormsApplication1.Properties.Settings>
<setting name="totalPageNum" serializeAs="String">
<value>10</value>
</setting>
<setting name="color" serializeAs="String">
<value>255, 255, 128</value>
</setting>
</WindowsFormsApplication1.Properties.Settings>
</userSettings>
<applicationSettings>
<WindowsFormsApplication1.Properties.Settings>
<setting name="ip" serializeAs="String">
<value>127.0.0.1</value>
</setting>
<setting name="url" serializeAs="String">
<value>http://my2/web/zh/</value>
</setting>
</WindowsFormsApplication1.Properties.Settings>
</applicationSettings>
string ip = Properties.Settings.Default.ip;
string url = Properties.Settings.Default.url;
int totalPageNum = Properties.Settings.Default.totalPageNum;
Color color = Properties.Settings.Default.color;
P.S applicationSettings 與 userSettings 皆可在執行期間新增刪除修改 Config 參數, 尤其 userSettings 標籤內的資訊通常是由 User 操作 GUI 的行為動態的修改, 但各個 User 的行為眾多, 且 User 數量也不少, 因此建議用 INI File 取代(實作 INI: http://www.dotblogs.com.tw/brian/archive/2013/07/15/109995.aspx)