How do you modify an arbitrary app.config file?

P

Paul

(This is all motivated by problems with Vista file protection. Get rid of Vista and boom, no problems, the users can edit the config files as always.)

I want to modify settings for an application. The application settings are in custom sections created by visual studio, <applicationSettings> for example. These are the project settings in Settings.settings.
I have yet to find a way to modify and save these settings from another app. You get part way into the the configuration namespaces and the API just shuts you down. There is no way to edit the settings. Do you know of any sample code to edit the settings in another app?

This code lets me delete a section, but this is clearly not what I want to do. I want to edit the values in the section.

ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = @"SomeOtherApp.exe.config";
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
ConfigurationSection mySection = config.GetSection("applicationSettings");
config.SectionGroups.Clear();
config.Save(ConfigurationSaveMode.Full);

I've yet to see any sample code that performs this obvious function.
 
P

Paul

This is evidently how you do this. But I have no idea if it works on Vista. This little configurator needs a reference to every dll referenced in the config file.

private void Form1_Load(object sender, EventArgs e)
{
// The UI is specific to the app, but allow user to
// choose the file.
OpenFileDialog fileDialog = new OpenFileDialog();
fileDialog.Filter = "Config Files(*.config)|*.config";
DialogResult res = fileDialog.ShowDialog(this);
if(res != DialogResult.OK)
return;

ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = fileDialog.FileName;
config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
section = (ClientSettingsSection) config.GetSection(
"applicationSettings/MyApp.Properties.Settings");

string productID = section.Settings.Get("ProductID").Value.ValueXml.InnerText;
textBoxProductId.Text = productID;

bool showDbOpenMenu =
Convert.ToBoolean(section.Settings.Get("ShowDatabaseOpenMenu").Value.ValueXml.InnerText);
bool showStylingMenu =
Convert.ToBoolean(section.Settings.Get("ShowAppStylingMenu").Value.ValueXml.InnerText);
bool showAttributeMenu =
Convert.ToBoolean(section.Settings.Get("ShowAttributeEditMenu").Value.ValueXml.InnerText);

checkedListBox1.Items.Add("Show database open menu item", true);
checkedListBox1.Items.Add("Show application styling menu item", true);
checkedListBox1.Items.Add("Show attribute editing menu item", true);

checkedListBox1.SetItemChecked(0, showDbOpenMenu);
checkedListBox1.SetItemChecked(1, showStylingMenu);
checkedListBox1.SetItemChecked(2, showAttributeMenu);
}

private void buttonOK_Click(object sender, EventArgs e)
{
section.Settings.Get("ProductID").Value.ValueXml.InnerText = textBoxProductId.Text.Trim();

section.Settings.Get("ShowDatabaseOpenMenu").Value.ValueXml.InnerText =
checkedListBox1.GetItemChecked(0).ToString();
section.Settings.Get("ShowAppStylingMenu").Value.ValueXml.InnerText =
checkedListBox1.GetItemChecked(1).ToString();
section.Settings.Get("ShowAttributeEditMenu").Value.ValueXml.InnerText =
checkedListBox1.GetItemChecked(2).ToString();

config.Save(ConfigurationSaveMode.Modified, true);

Close();
}
 
M

Matt Lacey

This is evidently how you do this. But I have no idea if it works on Vista. This little configurator needs a reference to every dll referenced in the config file.

private void Form1_Load(object sender, EventArgs e)
{
// The UI is specific to the app, but allow user to
// choose the file.
OpenFileDialog fileDialog = new OpenFileDialog();
fileDialog.Filter = "Config Files(*.config)|*.config";
DialogResult res = fileDialog.ShowDialog(this);
if(res != DialogResult.OK)
return;

ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = fileDialog.FileName;
config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
section = (ClientSettingsSection) config.GetSection(
"applicationSettings/MyApp.Properties.Settings");

string productID = section.Settings.Get("ProductID").Value.ValueXml.InnerText;
textBoxProductId.Text = productID;

bool showDbOpenMenu =
Convert.ToBoolean(section.Settings.Get("ShowDatabaseOpenMenu").Value.ValueX-ml.InnerText);
bool showStylingMenu =
Convert.ToBoolean(section.Settings.Get("ShowAppStylingMenu").Value.ValueXml-.InnerText);
bool showAttributeMenu =
Convert.ToBoolean(section.Settings.Get("ShowAttributeEditMenu").Value.Value-Xml.InnerText);

checkedListBox1.Items.Add("Show database open menu item", true);
checkedListBox1.Items.Add("Show application styling menu item", true);
checkedListBox1.Items.Add("Show attribute editing menu item", true);

checkedListBox1.SetItemChecked(0, showDbOpenMenu);
checkedListBox1.SetItemChecked(1, showStylingMenu);
checkedListBox1.SetItemChecked(2, showAttributeMenu);

}

private void buttonOK_Click(object sender, EventArgs e)
{
section.Settings.Get("ProductID").Value.ValueXml.InnerText = textBoxProductId.Text.Trim();

section.Settings.Get("ShowDatabaseOpenMenu").Value.ValueXml.InnerText =
checkedListBox1.GetItemChecked(0).ToString();
section.Settings.Get("ShowAppStylingMenu").Value.ValueXml.InnerText =
checkedListBox1.GetItemChecked(1).ToString();
section.Settings.Get("ShowAttributeEditMenu").Value.ValueXml.InnerText =
checkedListBox1.GetItemChecked(2).ToString();

config.Save(ConfigurationSaveMode.Modified, true);

Close();



}- Hide quoted text -

- Show quoted text -

Or you put such settings in a separate file which can be accesed by
other applications.
 
P

Paul

I would gladly do that. But where do you put files like this on vista? The settings have to be accessible r/w to admin, and apply to all users of the application. The directory has to be one of the standard ones so the application will know where to find it. And doing this, you lose the convenience of the Properties.Settings.Default... values.
 
M

Matt Lacey

I would gladly do that. But where do you put files like this on vista? The settings have to be accessible r/w to admin, and apply to all users of the application. The directory has to be one of the standard ones so the application will know where to find it. And doing this, you lose the convenience of the Properties.Settings.Default... values.




- Show quoted text -

Such a file should be in the localized equivalent of 'C:\Documents and
Settings\All Users\Application Data\'
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top