1. First you should add a App.config to your C# project
- Right click on your project –> Add –>New Item
- From Following window chose Application Configuration file
- Visual studio will add a XML file with following content
2. Add following content in between tags of App.config
3. Now you can store the connection string from following code (you should enter the connection string to a text box in your application named as txtConString)
private void EncryptConStr()
{
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings["connectionString"].Value = txtConString.Text;
config.Save(ConfigurationSaveMode.Modified);
EncryptSection("appSettings");
}private void EncryptSection(String SectionName)
{Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ConfigurationSection section = config.GetSection(SectionName);if (section != null)
{
if (!section.IsReadOnly())
{
section.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");
section.SectionInformation.ForceSave = true;
config.Save(ConfigurationSaveMode.Modified);
}
}
}
4. And you can view Connection string in App.config using following code
NOTE: For this example you should add a reference to System.Configuration from your project.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
txtConString.Text = config.AppSettings.Settings["connectionString"].Value.Trim().ToString();
* To Download source project click here
No comments:
Post a Comment