PDA

View Full Version : تغییر دادن تگی که در درون فایل app.config است؟



hanis29268
جمعه 23 اسفند 1392, 16:01 عصر
سلام،
بنده یه برنامه دارم که در درون آن فایل App.config وجود داره، یه connection داخلش دارم که میخوام تغییرش بدم، کسی میدونه چطور میشه اونو تغییرش داد؟؟
کد:
<?xml version="1.0"?><configuration>
<appSettings>
<add key="connectionString" value="Data Source=.\sql2008;Initial Catalog=dbCar;Integrated Security=True"/>
</appSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
</configuration>



وقتی میخوام این connection رو بخونم از کد زیر استفاده میکنم، ولی وقتی میخوام تغییرش بدم مثلا datasource رو عوض کنم نمیدونم چطوری باید این کارو کنم؟؟
ConfigurationSettings.AppSettings[0].ToString();
با تشکر فراوان

mohammadsamadi1377
جمعه 23 اسفند 1392, 16:11 عصر
public static void UpdateConnectionString(string newCS, string ConnectionString) {
ConnectionString = "Mosque.Properties.Settings.DatabaseConnectionStrin g";
// Use This After Call It :
//Properties.Settings.Default.Reload();


Configuration config = System.Configuration.ConfigurationManager.OpenExeC onfiguration(
ConfigurationUserLevel.None);
//the full name of the connection string can be found in the app.config file
// in the "name" attribute of the connection string
config.ConnectionStrings.ConnectionStrings[ConnectionString].
ConnectionString = newCS;
//Save to file
config.Save(ConfigurationSaveMode.Modified);


//force changes to take effect so that we can start using
//this new connection string immediately
ConfigurationManager.RefreshSection(config.Connect ionStrings.SectionInformation.Name);




}

mohammadsamadi1377
جمعه 23 اسفند 1392, 16:13 عصر
http://stackoverflow.com/questions/14349734/set-connectionstring-at-runtime

http://stackoverflow.com/questions/502411/change-connection-string-reload-app-config-at-run-time

http://www.codeproject.com/Questions/394781/how-to-change-connection-string-at-run-time-in-csh

hanis29268
جمعه 23 اسفند 1392, 16:32 عصر
من کد زیر رو ابتدای برنامم نوشتم،
using System.Configuration;
ولی چیزی به نام Configuration یا ConfigurationManager ندارم، فقط اینارو دارم:
ConfigurationExceptionConfigurationException
IConfigurationSectionHandler
IConfigurationSystem
RecommendedAsConfigurableAttribute

mohammadsamadi1377
جمعه 23 اسفند 1392, 16:37 عصر
لینک ها رو نگاه کردی ؟؟؟؟

hanis29268
جمعه 23 اسفند 1392, 17:24 عصر
آره،
من کتابخونه system.configuration رو اضافه کردم، ولی هیچ کدم از properti ها رو نمیشناسه؟؟؟