PDA

View Full Version : سوال: چطوری تنظیمات را در App.config ذخیره کنیم؟



EhsanTC
سه شنبه 18 فروردین 1388, 22:43 عصر
سلام به دوستان

من برای ذخیره تنظیمات از روش زیر عمل می کنم

config = ConfigurationManager.OpenExeConfiguration(Applicat ion.ExecutablePath);
alarmSoundPath = config.AppSettings.Settings["AlarmPath"].Value;
config.Save(ConfigurationSaveMode.Modified);


اما همان طور که می دونید در c:\Document and settings ذخیره می شه.حالا من چه کار کنم که در همان فایلی که در فولدر Debug است ذخیره بشه؟

با تشکر

meysam_pro
چهارشنبه 19 فروردین 1388, 10:02 صبح
using System;
using System.Collections.Specialized;
using System.Collections.ObjectModel;
using System.Collections;
using System.Text;
using System.Configuration;

namespace Samples.AspNet
{
class UsingConfigurationManager
{

// Show how to use AppSettings.
static void DisplayAppSettings()
{

// Get the AppSettings collection.
NameValueCollection appSettings =
ConfigurationManager.AppSettings;

string[] keys = appSettings.AllKeys;

Console.WriteLine();
Console.WriteLine("Application appSettings:");

// Loop to get key/value pairs.
for (int i = 0; i < appSettings.Count; i++)

Console.WriteLine("#{0} Name: {1} Value: {2}",
i, keys[i], appSettings[i]);

}

// Show how to use ConnectionStrings.
static void DisplayConnectionStrings()
{

// Get the ConnectionStrings collection.
ConnectionStringSettingsCollection connections =
ConfigurationManager.ConnectionStrings;

Console.WriteLine();
Console.WriteLine("Connection strings:");


// Loop to get the collection elements.
IEnumerator conEnum =
connections.GetEnumerator();

int i = 0;
while (conEnum.MoveNext())
{
string name = connections[i].Name;
string connectionString = connections[name].ConnectionString;
string provider = connections[name].ProviderName;

Console.WriteLine("Name: {0}", name);
Console.WriteLine("Connection string: {0}", connectionString);
Console.WriteLine("Provider: {0}", provider);
}

}

// Show how to use OpenMachineConfiguration.
static void DisplayMachineConfigSections()
{
// Get the machine.config file.
Configuration machineConfig =
ConfigurationManager.OpenMachineConfiguration();

ConfigurationSectionCollection sections = machineConfig.Sections;

Console.WriteLine();
Console.WriteLine("Sections in machine.config:");

// Loop to get the sections machine.config.
foreach (ConfigurationSection section in sections)
{
string name = section.SectionInformation.Name;
Console.WriteLine("Name: {0}", name);
}

}


// Show how to use OpenExeConfiguration(ConfigurationUserLevel)
// and RefreshSection.
static void UpdateAppSettings()
{
// Get the configuration file.
System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration(
ConfigurationUserLevel.None);

// Add an entry to appSettings.
int appStgCnt =
ConfigurationManager.AppSettings.Count;
string newKey = "NewKey" + appStgCnt.ToString();

string newValue = DateTime.Now.ToLongDateString() + " " +
DateTime.Now.ToLongTimeString();

config.AppSettings.Settings.Add(newKey, newValue);

// Save the configuration file.
config.Save(ConfigurationSaveMode.Modified);

// Force a reload of the changed section.
ConfigurationManager.RefreshSection("appSettings");

}

// Show how to use OpenExeConfiguration(string).
static void DisplayAppSettingsRawXml()
{


// Get the application path.
string exePath = System.IO.Path.Combine(
Environment.CurrentDirectory, "ConfigurationManager.exe");

// Get the configuration file.
System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration(exePath) ;

// Get the AppSetins section.
AppSettingsSection appSettingSection = config.AppSettings;

// Display raw xml.
Console.WriteLine(appSettingSection.SectionInforma tion.GetRawXml());


}

// Show how to use GetSection.
static void DisplayAppSettingsSectionRawXml()
{

// Get the configuration file.
System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration(Configur ationUserLevel.None);

// Get the AppSetins section.
AppSettingsSection appSettingSection =
(AppSettingsSection)config.GetSection("appSettings");

// Display raw xml.
Console.WriteLine(appSettingSection.SectionInforma tion.GetRawXml());


}

// Show how to use OpenMappedMachineConfiguration.
static void DisplayMappedMachineConfigurationFileSections()
{
// Get the machine.config file.
Configuration machineConfig =
ConfigurationManager.OpenMachineConfiguration();

// Map to the machine configuration file.
ConfigurationFileMap configFile =
new ConfigurationFileMap(machineConfig.FilePath);
Configuration config =
ConfigurationManager.OpenMappedMachineConfiguratio n(configFile);

// Display the configuration file sections.
ConfigurationSectionCollection sections = config.Sections;

Console.WriteLine();
Console.WriteLine("Sections in machine.config:");

// Loop to get the sections machine.config.
foreach (ConfigurationSection section in sections)
{
string name = section.SectionInformation.Name;
Console.WriteLine("Name: {0}", name);
}

}


// Show how to use OpenMappedExeConfiguration.
static void DisplayMappedExeConfigurationFileSections()
{
// Get the application configuration file path.
string exeFilePath = System.IO.Path.Combine(
Environment.CurrentDirectory, "ConfigurationManager.exe.config");

// Map to the application configuration file.
ExeConfigurationFileMap configFile = new ExeConfigurationFileMap();
configFile.ExeConfigFilename = exeFilePath;
Configuration config =
ConfigurationManager.OpenMappedExeConfiguration(co nfigFile,
ConfigurationUserLevel.None);

// Display the configuration file sections.
ConfigurationSectionCollection sections = config.Sections;

Console.WriteLine();
Console.WriteLine("Sections in machine.config:");

// Loop to get the sections machine.config.
foreach (ConfigurationSection section in sections)
{
string name = section.SectionInformation.Name;
Console.WriteLine("Name: {0}", name);
}

}

static void Main(string[] args)
{

// Show how to use OpenExeConfiguration() and RefreshSection.
UpdateAppSettings();

// Show how to use AppSettings.
DisplayAppSettings();

// Show how to use ConnectionStrings.
DisplayConnectionStrings();

// Show how to use OpenExeConfiguration(string).
DisplayAppSettingsRawXml();

// Show how to use GetSection.
DisplayAppSettingsSectionRawXml();

// Show how to use OpenMappedMachineConfiguration.
DisplayMappedMachineConfigurationFileSections();

// Show how to use OpenMappedExeConfiguration.
DisplayMappedExeConfigurationFileSections();

// Show how to use OpenMachineConfiguration.
DisplayMachineConfigSections();

}
}

}




Inheritance Hierarchy اون از System.Object بود ولی پیدا نشد!:متفکر:

meysam_pro
چهارشنبه 19 فروردین 1388, 10:11 صبح
در ضمن اگه میخوای ConnectionString رو تو app.config ذخیره کنی تا بعدا بشه عوضش کرد، یه نگاه به این مقاله بنداز یک جستجوی ساده با استفاده از عملگر Like (http://www.vesal-behrouzi.ir/index.php/archives/7).

اگه با این مقاله دیتابیس رو درست کنی ، خود برنامه با app.config کار میکنه.