 |
|
 |
The article was written to demonstrate how to read and write user settings and I think it did a great job. Thanks Chad!
As I used the code to handle window sizing in an app, I discovered two cases that aren't handled in the code above. To handle these cases I added a little code, which I thought I'd share.
Case 1: The user maximizes the window. Next time she opens the app, she expects the window to be maximized, but the code above doesn't do that.
Case 2: The user lowers screen resolution or stops using a second monitor. This happens all the time when I undock my laptop. The code above may place the window where it can't be seen.
To solve for these two use cases I add a setting of type bool called MainFormMaximized and use this code:
private void MainForm_Load(object sender, EventArgs e) { if (Properties.Settings.Default.MainFormMaximized) { this.WindowState = FormWindowState.Maximized; } else { Rectangle ProposedWindowRect = new Rectangle(Properties.Settings.Default.MainFormPosition, Properties.Settings.Default.MainFormSize); bool WindowWillFit = false; foreach (Screen S in Screen.AllScreens) { if (S.WorkingArea.Contains(ProposedWindowRect)) { WindowWillFit = true; } } if (WindowWillFit) { this.Location = Properties.Settings.Default.MainFormPosition; this.Size = Properties.Settings.Default.MainFormSize; } else { this.Location = new Point(0, 0); this.Size = new Size(600, 400); } } }
private void MainForm_FormClosing(object sender, FormClosingEventArgs e) { Properties.Settings.Default.MainFormPosition = this.Location; Properties.Settings.Default.MainFormSize = this.Size; Properties.Settings.Default.MainFormMaximized = (this.WindowState == FormWindowState.Maximized); Properties.Settings.Default.Save(); }
Enjoy!
/Martin
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
I use a dialogbox on my startup form to find my Access database. I then want this to my connectionstring in app.config, but I get the failure connectionString is Readonly. How do I get it writeable?
Leif
|
| Sign In·View Thread·PermaLink | 1.00/5 |
|
|
|
 |
|
 |
Good article. I want to ask you that is it safe to save passwords like informations using this way ?
Thanks, Dan
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
No, Isn't safe!! MS wrote:"...Application settings has no built-in facility for encrypting information automatically. You should never store security-related information, such as database passwords, in clear text. If you want to store such sensitive information, you as the application developer are responsible for making sure it is secure. If you want to store connection strings, we recommend that you use Windows Integrated Security and not resort to hard-coding passwords into the URL." See "Limitations of Application Settings" on MSDN - http://msdn2.microsoft.com/en-us/library/k4s6c3a0.aspx
Have a nice day! Dmitriy
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
 |
Hello is possible add ToolStripMenuItem and saved to have with application settings???. I'm the created but not saved, when begin not change the aplication.. Saludos.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
jhon edison wrote: Hello is possible add ToolStripMenuItem and saved to have with application settings???.
There is a problem with PropertyBinding and ToolStripMenuItems. The code to load the Settings is correctly built by VisualStudio, but the code to save the settings isn't built, you'll have to do this by hand.
Look in the Designer code for your module. In the Dispose sub add code to save your settings. Ex, if you want to save the state of a checked button.
My.Settings.ctlEventLogViewer_btnErrors_Checked = btnErrors.Checked
That's all it takes.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
I have an application with several simple types: boolean, int, strings...and it seems to save these fine. But anything that has a serializeAs="XML" like a webproxy, or hashtable or whatever doesn't save. Anyone else having this problem?
JL
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
 |
|
|
 |
|
|
 |
|
 |
It's possible to have my.Setting from different classes?
I mean, I have a solution with 6 different projects on it. There are 5 classes which are Library classes, and one form project class.
The basic idea is to have a config file on the project, and all the libraries would use this config file values.
So the question is: Is there a way to have the object “My.Setting.” from the libraries loading the configuration of the win form project? Obviously I can reach “My.Setting” from the main class of the win form project, but I can’t find the way to reach it from the libraries. Any ideas ? Can anyone help me please? Thanks in advice
Another way, if anyone knows a better way to have those configuration options managed, also will be listened!!!  Jordi.
-- modified at 4:45 Friday 30th March, 2007
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
I am using the user settings to store some data. Every time I make some changes to the program, I will pass a new executable to the testor. The problem is when they replace the old executable with the new one. The user settings will be lost. Maybe becuase of different version of codes. Is there a walk around?
Thanks
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Thomas Fang wrote: Is there a walk around?
Yes. The My.Settings object has a method called Upgrade. If you call this, it will migrate your settings from your old version to the new version. To get this to work correctly you need to create a flag in your settings file that will tell the app when to upgrade. (You don't want to upgrade every time your app launches or you will loose your settings made after the upgrade.)
So, what I do is create a setting called CallSettingsUpgrade as boolean setting with the default value = to True. In the form load event, I check to see if this setting is True, if so, I call .Upgrade and then set this setting to False.
'If a new version is deployed, upgrade the settings. If My.Settings.CallSettingsUpgrade Then My.Settings.Upgrade() My.Settings.CallSettingsUpgrade = False End If
That will do what you need.
|
| Sign In·View Thread·PermaLink | 4.00/5 |
|
|
|
 |
|
 |
I've written a windows service in VB.NET 2005 using the application settings. The appl. settings file is located in the same dir as the exe (of the service). Still the service isn't reading the settings but is also not giving any error ! The service is running with the local administrator account. What should I do to make it running ??
-- modified at 6:36 Tuesday 11th July, 2006
|
| Sign In·View Thread·PermaLink | 5.00/5 |
|
|
|
 |
|
 |
Hi,
I did exactly the same thing and had exactly the same problem. Tried it with a console application as well - same problem - not reading the app.config file.
I import using System.Collections.Specialized; using System.Configuration;
Code (in the console version) is from an MS example:
static void ShowAppSettings() { string[] names = ConfigurationManager.AppSettings.AllKeys; NameValueCollection appStgs = ConfigurationManager.AppSettings; for (int i = 0; i < appStgs.Count; i++) { Console.WriteLine("#{0} Name: {1} Value: {2}", i, names[i], appStgs[i]); } }
static void Main(string[] args) {
ShowAppSettings(); }
I have a value in the app.config file, but it never appears in the string[] names array.
Any ideas?
Thanks
Philip
|
| Sign In·View Thread·PermaLink | 4.00/5 |
|
|
|
 |
|
 |
You need to set the working directory for the service like so.
// Define working directory (For a service, this is set to System) Process pc = Process.GetCurrentProcess(); Directory.SetCurrentDirectory(pc.MainModule.FileName.Substring(0,pc.MainModule.FileName.LastIndexOf(@"\") ));
You can set this in the service's OnStart method.
|
| Sign In·View Thread·PermaLink | 5.00/5 |
|
|
|
 |
|
 |
also you can use object propertys -> ApplicationSetting -> Property binding, select the property and bind it!
this method is easyer then saving each object's states on unload etc...
|
| Sign In·View Thread·PermaLink | 1.00/5 |
|
|
|
 |
|
 |
Hi,
Storing the user settings under a directory that contains the version of the application seems strange. When the application evolves and goes from one version to another, all the user settings are back to their default values. How can we handle the recovery of the old settings ?
David
|
| Sign In·View Thread·PermaLink | 4.43/5 |
|
|
|
 |
|
 |
I use AssemblyInfoTask which automatically increments AssemblyVersion after each compile and that also means each time new settings. I was googling for a way to change the user.config file location (to not include the version details), but found out it is not possible. However, what you can is you can call ApplicationSettingsBase.Upgrade(). This will read the old settings and store them into the new file.
You only need to call upgrade() the first time you application is launched. Crete a new setting called CallUpgrade and give it a default value of true. In the code do this check:
if (Properties.Settings.Value.CallUpgrade) { Properties.Settings.Value.Upgrade(); Properties.Settings.Value.CallUpgrade = false; }
I still end up with a couple of new config files every day, but at least it works.
Matjaz
|
| Sign In·View Thread·PermaLink | 4.73/5 |
|
|
|
 |
|
|
 |
|
 |
I've just spent hours trying to get this obfuscated garbage working and it's not supported in C#! WTF?
|
| Sign In·View Thread·PermaLink | 2.00/5 |
|
|
|
 |
|
|
 |
|
 |
Figured out that MUCH later but still irate at the VB only focus. Saw an MSDN tech VB only article on the same thing around that time.
|
| Sign In·View Thread·PermaLink | 1.20/5 |
|
|
|
 |