How to save Application Settings to a different location and file name - c#

C# Windows Forms.
I have successfully used Application Settings to save and load user settings for the values of controls on my form.
I have a checkbox and code where I can set whether this happens automatically (at application start and close), or not.
I also have a menu so I can load them and save them during runtime.
This is all using the default user.config.
Example.
In Application settings I have (for one of the items, which is a radio button called RbBitRate6Mbps):
Name: BitRate6Mbps
Type: String
Scope: User
Value: False
To save the settings I have a menu item, Save Defaults. This runs:
if (RbBitRate6Mbps.Checked == true)
{
Settings.Default["BitRate6Mbps"] = "True";
}
else
{
Settings.Default["BitRate6Mbps"] = "False";
}
Settings.Default.Save();
To load the settings back in I have a menu item, Load Defaults. This runs:
if (Settings.Default["BitRate6Mbps"].ToString() == "True")
{
RbBitRate6Mbps.Checked = true;
}
else
{
RbBitRate6Mbps.Checked = false;
}
There are about 10 other controls I save and load (text boxes, check boxes and radio buttons), in the rest of the above code.
This is all working with no issues.
Now I would like to have several sets of these settings, each will contain some identical values and some different ones.
Each will have a different file name and be saved into a custom location (which will be the app folder, by default).
I do not mind what format the file is (xml, ini, etc), but if it is the same as the default, this would be good.
I have created new menu items, Save Custom Settings and Load Custom Settings.
I have added a SaveFileDialog and a LoadFileDialog to hopefully be used for the above.
But if these are not necessay, that is good too.
This is where I have become stuck.
I have been searching for days for a clear example of how to do this.
I have been unable to find much. What I have found, I have been unable to understand the documentation.
I am thinking loading the settings back in will be the easier part?
But I also think, for saving the file, using:
Settings.Default.Save();
Will not accomplish my aims as it will just write to the default user.config file ?
Is what I want to do possible?
If so does anyone have any instructions and example code?

Update. I have installed a new Settings Provider and it is working well. It saves the XML to the app folder. I have also set up INI files to save the settings. Using both a custom path and custom file name. It allows for multiple INI files to be created. This also works extremely well.
Edit: Updated code (and instructions) so it is no longer necessary to create any custom folder manually. If it does not exist, it will be created.
The XML Settings Provider developers project is located here:
Settings Providers on Github
The INI file developers project (and demo) is located here:
C# WinForms Ini File demo on Github
Below are the instructions for setting up the new Settings Provider with an XML file and following that are the instructions for saving the settings to INI files (both types can be used in the same project at the same time, as I am doing here):
Using a new Settings Provider to save settings in an XML file:
1.
Setup the Application Settings (in Solution Explorer, right-click on the App. Then Select: Properties. Then open: Settings).
Name: txtFullName
Type: String
Scope: User
Value: John Doe
Name: txtSchool
Type: String
Scope: User
Value: Oxford
Name: txtClass
Type: String
Scope: User
Value: 4B
Name: chkActiveStudent
Type: bool
Scope: User
Value: True
2.
Install, from NuGet the new Settings Provider (in Solution Explorer, right-click on: References. Then Select: Manage NuGet Packages. Then search for: PortableSettingsProvider. Install it).
3.
In Program.cs modify static void Main(). Add to it the lines below.
//PortableSettingsProvider.SettingsFileName = "portable.config";
//PortableSettingsProvider.SettingsDirectory = "c:\\\testconfig\\\school";
//System.IO.Directory.CreateDirectory(PortableSettingsProvider.SettingsDirectory);
PortableSettingsProvider.ApplyProvider(Properties.Settings.Default);
If accepting the default settings (the config file, portable.config, will be created in the applications folder), a properly edited static void Main() entry would look like the below.
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//PortableSettingsProvider.SettingsFileName = "portable.config";
//PortableSettingsProvider.SettingsDirectory = "c:\\testconfig\\school";
//System.IO.Directory.CreateDirectory(PortableSettingsProvider.SettingsDirectory);
PortableSettingsProvider.ApplyProvider(Properties.Settings.Default);
Application.Run(new MyTestApp());
}
3a.
To choose a different filename and location, remove the comments (//) and change to your preference for filename and location (double slashes are needed between the folder names). In this example I use settings.config as the filename and c:\testconfig\school as the path). In this case a properly edited static void Main() entry would look like the below.
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
PortableSettingsProvider.SettingsFileName = "settings.config";
PortableSettingsProvider.SettingsDirectory = "c:\\testconfig\\school";
System.IO.Directory.CreateDirectory(PortableSettingsProvider.SettingsDirectory);
PortableSettingsProvider.ApplyProvider(Properties.Settings.Default);
Application.Run(new MyTestApp());
}
3b.
If you would like the settings directory to be created in a subfolder of the applications working directory, then change the code to include the subfolder name (in this example I use settings.config as the filename and Settings as the subfolder). In this case a properly edited static void Main() entry would look like the below.
static void Main()
{
Application.EnableVisualStyles();
PortableSettingsProvider.SettingsFileName = "settings.config";
var strSettingsDirectory = Directory.GetCurrentDirectory() + "\\Settings";
System.IO.Directory.CreateDirectory(strSettingsDirectory);
PortableSettingsProvider.SettingsDirectory = strSettingsDirectory;
PortableSettingsProvider.ApplyProvider(Properties.Settings.Default);
Application.Run(new MyTestApp());
}
4.
Still in Program.cs, add the following line to the bottom of the existing using section.
using Bluegrams.Application;
5.
On the form create some controls (these will correspond to the Application Settings made in step 1).
TextBox. Name: txtFullName
TextBox. Name: txtSchool
Textbox. Name: txtClass
Checkbox. Name: chkActiveStudent
Button. Name: btnLoadSettings Text: Load Config
Button. Name: btnSaveSettings Text: Save Config
6.
Enter the code for the Load Config buttons click events (btnLoadSettings).
private void btnLoadSettings_Click(object sender, EventArgs e)
{
txtName.Text = Properties.Settings.Default.txtName.ToString();
txtSchool.Text = Properties.Settings.Default.txtSchool.ToString();
txtClass.Text = Properties.Settings.Default.txtClass.ToString();
if (Properties.Settings.Default.chkActiveStudent == true)
{
chkActiveStudent.Checked = true;
}
else
{
chkActiveStudent.Checked = false;
}
}
7.
Enter the code for the Save Config buttons click events (btnSaveSettings).
private void btnSaveSettings_Click(object sender, EventArgs e)
{
Properties.Settings.Default.txtName = txtName.Text;
Properties.Settings.Default.txtSchool = txtSchool.Text;
Properties.Settings.Default.txtClass = txtClass.Text;
if (chkActiveStudent.Checked == true)
{
Properties.Settings.Default.chkActiveStudent = true;
}
else
{
Properties.Settings.Default.chkActiveStudent = false;
}
Properties.Settings.Default.Save();
}
8.
That’s it.
Run the app.
Load the settings using the first button).
Make some changes to the text on the controls.
Save them using the second button.
If you have not created a custom filename and/or path then in your app folder there should be a new file: portable.config.
Mine is located at: C:\Users\flakie\source\repos\TestApp\TestApp\bin\Debug
You can open the file in an txt/xml editor to verify the values were set.
If you run the app again, and load settings, you should see the new values.
The Code for Saving Settings to Multiple INI Files
The following instructions are easier to implement.
They do not require you to set-up the Application Settings as they are not used.
The settings will be saved in 1 or more INI files.
The path can be changed on-the-fly (though a default is set), as can the file name for the INI file.
Again it will be using the applications own folder as the default destination (for the INI files).
You will need to modify the Form.cs file only.
The demo (from the link above) is easy to understand and was enough to provide me with the knowledge to create this example (and I am a C# novice).
9.
Install, from NuGet, the INI files package (in Solution Explorer, right-click on: References. Then Select: Manage NuGet Packages. Then search for: PeanutButter.INI. Install it).
10.
In your Form.cs file (Form1.cs if you have not changed the name), add the following line to the bottom of the existing using section.
using PeanutButter.INIFile;
11.
In your Form.cs file, directly under the line, public partial class Form1 : Form, add the following single line of code.
private IINIFile _loadedConfig;
It should look like the below.
public partial class Form1 : Form {
private IINIFile _loadedConfig;
12.
On the form create two more buttons.
Button. Name: btnOpenIniFile Text: Open INI
Button. Name: btnSaveIniFile Text: Save INI
13.
Enter the code for the Open INI buttons click event (btnOpenIniFile).
private void btnOpenIniFile_Click(object sender, EventArgs e) {
using(OpenFileDialog OpenFileIni = new OpenFileDialog()) {
var strSettingsDirectory = Directory.GetCurrentDirectory() + "\\Settings";
System.IO.Directory.CreateDirectory(strSettingsDirectory);
OpenFileIni.InitialDirectory = strSettingsDirectory;
OpenFileIni.Filter = "INI File|*.ini";
OpenFileIni.RestoreDirectory = true;
OpenFileIni.CheckFileExists = true;
OpenFileIni.CheckPathExists = true;
OpenFileIni.Title = "Open an INI Settings File";
if (OpenFileIni.ShowDialog() == DialogResult.OK)
{
_loadedConfig = new INIFile(OpenFileIni.FileName);
txtName.Text = _loadedConfig.HasSetting("UserSettings", "txtName") ? _loadedConfig["UserSettings"]["txtName"] : "";
txtSchool.Text = _loadedConfig.HasSetting("UserSettings", "txtSchool") ? _loadedConfig["UserSettings"]["txtSchool"] : "";
txtClass.Text = _loadedConfig.HasSetting("UserSettings", "txtClass") ? _loadedConfig["UserSettings"]["txtClass"] : "";
if (_loadedConfig["UserSettings"]["chkActiveStudent"] == "Checked")
{
chkActiveStudent.Checked = true;
}
else
{
chkActiveStudent.Checked = false;
}
}
}
}
14.
Enter the code for the Save INI buttons click event (btnSaveIniFile).
private void btnSaveIniFile_Click(object sender, EventArgs e)
{
using (SaveFileDialog SaveFileIni = new SaveFileDialog())
{
var strSettingsDirectory = Directory.GetCurrentDirectory() + "\\Settings";
System.IO.Directory.CreateDirectory(strSettingsDirectory);
SaveFileIni.InitialDirectory = strSettingsDirectory;
SaveFileIni.Filter = "INI File|*.ini";
SaveFileIni.Title = "Save an INI Settings File";
if (SaveFileIni.ShowDialog() == DialogResult.OK)
{
_loadedConfig = new INIFile(SaveFileIni.FileName);
_loadedConfig["UserSettings"]["txtName"] = txtName.Text;
_loadedConfig["UserSettings"]["txtSchool"] = txtSchool.Text;
_loadedConfig["UserSettings"]["txtClass"] = txtClass.Text;
if (chkActiveStudent.Checked == true)
{
_loadedConfig["UserSettings"]["chkActiveStudent"] = "Checked";
}
else
{
_loadedConfig["UserSettings"]["chkActiveStudent"] = "UnChecked";
}
_loadedConfig.Persist();
}
}
}
15.
That’s it. Run the app.
Make some changes to the text on the textbox controls. Save them using the Save INI button.
You will be prompted for a file name. It can be anything you like (but in this example I used the name of the person I setup in the first textbox.
You do not need to enter the ini file extension. You can change the location if you wish.
Make some more changes to the text in the textBoxes but do not save them.
Click on the Open INI button. Browse to the ini file you just saved and open it.
The text you just modified, without saving, should now be changed to the text you saved into the ini file.

Use OpenExeConfiguration function to read settings and then Add/Update key values before Save it back. Finally you can ConfigurationManager.RefreshSection to refresh settings of a particular section.
Example at Link
static void AddUpdateAppSettings(string key, string value)
{
try
{
var configFile = ConfigurationManager
.OpenExeConfiguration(ConfigurationUserLevel.None);
var settings = configFile.AppSettings.Settings;
if (settings[key] == null)
{
settings.Add(key, value);
}
else
{
settings[key].Value = value;
}
configFile.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection(configFile.AppSettings.SectionInformation.Name);
}
catch (ConfigurationErrorsException)
{
Console.WriteLine("Error writing app settings");
}
}

Related

How can I pass a file/file path from one Button_Click event to another in C#?

I have a fileupload control (FileUpload1) in my webform which I use to load an excel file. I use a button called btn_open to display it in a gridview on click.
I also save the file using FileUpload1.SaveAs() method in a server folder.
Now I have another button called btn_edit which on click needs to use the same
file that I just loaded to do another set of operations.
How do I pass this file/file path from btn_open_Click to btn_edit_Click? I do not want to specify the exact location on the code. There will be multiple times I will open new excel files so I don't want to specify the file path to the server for every new file.This needs to happen programatically. Also, I want to avoid using Interop if its possible.
The following code snippet might make things more clear as to what I want to do.
protected void btn_open_Click(object sender, EventArgs e)
{
string fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName);
if (fileExtension.ToLower() == ".xlsx" || fileExtension.ToLower() == ".xls")
{
string path = Path.GetFileName(FileUpload1.FileName); //capture the file name of the file I have uploaded
path = path.Replace(" ", ""); // if there is any spacing between the file name it will remove it
FileUpload1.SaveAs(Server.MapPath("~/ExcelFile/") + path); //saves to Server folder called ExcelFile
String ExcelPath = Server.MapPath("~/ExcelFile/") + path; // Returns the physical file path that corresponds to the specified virtual path.
.
.
*code to display it in gridview*
.
.
}
else
{
Console.Writeline("File type not permissible");
}
}
protected void btn_edit_Click(object sender, EventArgs e)
{
//HOW DO I PASS THE ABOVE FILE HERE PROGRAMATICALLY WITHOUT SPECIFYING IT'S EXACT LOCATION ON THE SERVER??
}
Using Session variable helped me solve my problem. Hopefully it will be helpful to other people who might encounter similar issue.
On the first button (in my case btn_open_Click), add:
Session["myXlsPath"] = ExcelFilePath;
Note: "myXlsPath" is just a name I created for my Session variable. ExcelFilePath is the path of my excel file that I want to pass to the other button.
On the button you want to pass the file path to (in my case btn_edit_Click), add:
string ExcelFilePath = (string)Session["myXlsPath"];

Inhibit Input Validation on CommonOpenFileDialog in Win API Pack

This is a WPF app with the latest version of the .NET framework and VS2015
on a Win 10 box.
I am trying to use the "CommonOpenFileDialog" from the Windows API code pack 1.1
to allow the user to establish a folder in which to do some stuff. The folder
can be either an existing folder, or a new one that the user specifies.
If the user wants to create a new folder, then I want them to be able to specify
the folder by editing the text within the "Folder:" textbox at the bottom of the
dialog. Within this context, the dialog would just be a means by which to
navigate to the folder in which the new one is to be created. My plan is to
validate the input within my code to check for a valid (existing) path, and
simply create the path if it does not exist.
Here is the code:
private void test1_folderSelectorDialog ()
{
if (CommonFileDialog.IsPlatformSupported)
{
var folderSelectorDialog = new CommonOpenFileDialog();
folderSelectorDialog.EnsureReadOnly = false;
folderSelectorDialog.IsFolderPicker = true;
folderSelectorDialog.Multiselect = false;
folderSelectorDialog.EnsureValidNames = false;
folderSelectorDialog.EnsurePathExists = false;
folderSelectorDialog.EnsureFileExists = false;
folderSelectorDialog.InitialDirectory
= "C:\\My_Initial_Directory";
folderSelectorDialog.Title = "test1_folderSelectorDialog";
if (folderSelectorDialog.ShowDialog() == CommonFileDialogResult.Ok)
TxBx_folder.Text = folderSelectorDialog.FileName;
this.Focus();
}
else
MessageBox.Show ("CommonFileDialog is not supported");
}
When I run the dialog and modify the text within the "Folder:" textbox,
then press "Select Folder", the dialog validates the input and issues a
dialog popup with the message:
"Path does not exist. Check the path and try again."
Please note that I have set "EnsureValidNames", "EnsurePathExists", and
"EnsureFileExists" to "false". (If they do not control dialog validation,
then what are they there for?)
I can right-click on the dialog window and use "new > Folder" to create a
new folder (which is what I'll have to do if I cannot resolve this issue),
but I'd rather do it the way that I am trying to do it, as it seems much
easier and more intuitive to do it that way.
How do I get the silly thing to shaddup and just accept the input without
passing judgement upon it?
Thanks!
If you want the user to select only the folder, then below code is enough
CommonOpenFileDialog dialog = new CommonOpenFileDialog()
dialog.IsFolderPicker = true
if (dialog.ShowDialog() == CommonFileDialogResult.Ok)
{
filesPath = dialog.FileName
}
I believe below things are not required
folderSelectorDialog.Multiselect = false
folderSelectorDialog.EnsureValidNames = false
folderSelectorDialog.EnsurePathExists = false
folderSelectorDialog.EnsureFileExists = false

WinForm settings not saving on Windows Shut down

I have a Winform program I wrote for a university so they could run test files.
The program is targeted at .NET 4.5 and was developed and run on Windows 7.
To make the running of files easy for the technicians I added the ability to save the paths to the test files. The file path is stored in a settings file (ProgSettings.settings) under the variable "RunNames".
When the save button is pressed I run a validation to ensure the test file at the end of the path exists and is valid before adding it to the RunNames list:
/*
*Function: SaveButton_Click(sender, e)
*Notes: responds to the button click "Save"
* tries to save file (if valid) to settings
*/
private void SaveButton_Click(object sender, EventArgs e)
{
if (ValidateFile(TrackFileName.Text))
{
string displayName = updateLinkNames(TrackFileName.Text).ToUpper();
if (!ListOfTracks.Items.Contains(displayName))
{
ListOfTracks.Items.Add(displayName);
runNames.Add(TrackFileName.Text);
}
updateSettings();
}
}
/*
*Function: ValidateFile(fileName)
*Notes: called from save button click
* checks validity of the file path
*/
private bool ValidateFile(string fileName)
{
fileName = fileName.ToUpper();
FileToRun.FileName = fileName;
validFile = System.IO.File.Exists(fileName);
if(fileName != null && fileName != "" && fileName.EndsWith(".TXT") && validFile)
{
return true;
}
else
{
return false;
}
}
/*
*Function: updateSettings()
*Notes: updates the settings with the valid path
*/
private void updateSettings()
{
string value = String.Join(",", runNames.Select(i => i.ToString()).ToArray());
ProgSettings.Default.RunNames = value;
ProgSettings.Default.Save();
}
(For those of you unfamiliar with .settings they store the basic types available on C#, there are ways to include arrays as well but the simplest solution is to concatenate the different parts of the array in a string with a separator - in my case ",").
Then when I load the program again I populate a list of checkboxes using the a getSavedFiles() function below.
private List<string> getSavedFiles()
{
string localRunNames = ProgSettings.Default.RunNames.ToUpper();
string[] arr = localRunNames.Split(',').Select(s => Convert.ToString(s)).ToArray();
return arr.Cast<String>().ToList();
}
The settings works correctly on both my work computer and my personal laptop I did testing on. However, now the program is in use at the university they are saying that after shutting down the computer the next time the run the program all the saved file paths are gone.
Is this due to how to how the university managers users accounts on the computer? Or is there some other reason for this?
Was there another alternative to storing setting information for cases where .settings files don't work? (The only other solution I can think of is having the program write a config.txt file which I would prefer not to do encase the application is used somewhere it won't having writing permissions)
Below is a link to a question about the best practice to save application settings:
Best practice to save application settings

How to dynamicaly load and save user settings in WPF multi window application?

I'm trying to save user setting in a WPF application. I want to save their Window position and size. To do so, I inspire myself of this.
I change a little the save code to do the following :
var oBaseProp = Properties.Settings.Default.Properties["baseProp"];
Properties.Settings.Default["baseProp"] = "1";
System.Configuration.SettingsProperty property = new System.Configuration.SettingsProperty("W_" + this.GetType().FullName + "_PLACEMENT"
, typeof(string), oBaseProp.Provider, false, GetPlacement(this), System.Configuration.SettingsSerializeAs.String, oBaseProp.Attributes,
false, false);
Properties.Settings.Default.Properties.Add(property);
Properties.Settings.Default["W_" + this.GetType().FullName + "_PLACEMENT"] = GetPlacement(this);
Properties.Settings.Default.Save();
BaseProp is a string property defined in my settings which is useless and just used to retrieve provider and other things.
Everything works fine for saving, I have a new file in C:\Users\christophe.mom.DOMAINE-EPSILOG\AppData\Local\Epsilog\MSSante_GUI.vshost.exe_Url_fkwxpmo4exiyrjk2vha2qu2upx5sgrvn\1.0.0.0 path, user.config that contains the desired data.
The thing is, I cannot load this file as settings file :
void StandardWindow_SourceInitialized(object sender, EventArgs e)
{
try
{
Properties.Settings.Default.Reload();
var sPlacement = (string)Properties.Settings.Default.Properties["W_" + this.GetType().FullName + "_PLACEMENT"].DefaultValue;
if (!string.IsNullOrWhiteSpace(sPlacement))
{
this.SetPlacement(this, sPlacement);
}
}
catch { }
}
I don't understand why my actual settings are not loaded. I tried in execution (not debug) mode too, and still no results.
Is someone have an idea ?
Thanks !
Well, in fact, this works only on execution mode compiled in release (in my case).

Simple silverlight open-file-dialog errors

A while back I wrote a silverlight user control which had a csv import/export feature. This has been working fine, until recently I discovered it erroring in one scenario. This may have been due to moving to Silverlight 3.
The Error:
Message: Unhandled Error in Silverlight 2 Application
Code: 4004
Category: ManagedRuntimeError
Message: System.Security.SecurityException: Dialogs must be user-initiated.
at System.Windows.Controls.OpenFileDialog.ShowDialog()
at MyControl.OpenImportFileDialog()
at ...
The Code:
private void BrowseFileButton_Click(object sender, RoutedEventArgs e)
{
if (string.IsNullOrEmpty(lblFileName.Text))
{
if (MessageBox.Show("Are you sure you want to change the Import file?", "Import", MessageBoxButton.OKCancel) == MessageBoxResult.Cancel)
{
return;
}
}
EnableDisableImportButtons(false);
var fileName = OpenImportFileDialog();
lblFileName.Text = fileName ?? string.Empty;
EnableDisableImportButtons(true);
}
private string OpenImportFileDialog()
{
var dlg = new OpenFileDialog { Filter = "CSV Files (*.csv)|*.csv" };
if (dlg.ShowDialog() ?? false)
{
using (var reader = dlg.File.OpenText())
{
string fileName;
//process the file here and store fileName in variable
return fileName;
}
}
}
I can open an import file, but if i want to change the import file, and re-open the file dialog, it errors. Does anyone know why this is the case?
Also, I am having trouble debugging because placing a breakpoint on the same line (or prior) to the dlg.ShowDialog() call seems to cause this error to appear as well.
Any help would be appreciated?
You do two actions on one user click.
You show a messagebox which effectively uses your permission to show a dialog on user action.
You then try to show the dialog, since this is a second dialog on user action it's not allowed.
Get rid of the confirmation dialog and you'll be fine.
Remove Break Points before if (dlg.ShowDialog() ?? false) code will run its work for me.

Categories