I have a windows forms desktop application. I am using a settings variable in resource named Settings.Default.Code
So I set the value initial value in Form->Properties->Settings.Code="123"
And I have changed the value in application using code like this:
private void button1_Click(object sender, EventArgs e)
{
Settings.Default.Code = "ABC";
Settings.Default.Save();
}
After button click the Code value is same as following image
:
But if I get the value in code Settings.Default.Code is "ABC"
When I created a setup project and release the application, the value of this settings should be "123" but it is "ABC". The initial value is not get from first setted value. Why? How can I solve this problem?
Add this line to the beginning of your program:
Settings.Default.Reset();
More info: ApplicationSettingsBase.Reset Method
As #Peter Duniho mentioned, the problem is probably that the setting is already saved to your profile on your computer. Try to delete the config file before you test again:
C:\Users{username}\AppData\Local\{ApplicationName}
or
C:\Users{username}\AppData\Roaming\{ApplicationName}
Related
When a user runs the program for the first time I want a message box to show up.
I was thinking of something like this:
private void Form1_Load(object sender, EventArgs e)
{
if(firstTime)
{
MessageBox.Show("Welcome");
}
How could I get my program to display a message box when a user launches the program for the first time in c#?
You would need to store that information somewhere
File
System registry
Database
Settings in application
Then read the value, and setup the firstTime flag prior to check.
You can add a parameter to the application settings.
Go to the solution explorer in the section Properties and double click on Settings.settings.
Add a parameter named for example IsFirstLaunch and set type to bool with value True.
Then you can write:
if ( Properties.Settings.Default.IsFirstLaunch )
{
Properties.Settings.Default.IsFirstLaunch = false;
Properties.Settings.Default.Save();
MessageBox.Show("Welcome");
}
The settings are stored in:
c:\Users\{UserName}\AppData\Local\{Assembly CompanyName}\{Assembly Name}.Url__________
So be careful to set Assembly CompanyName in the AssemblyInfo.cs in the same section.
Assembly Name is from the application project properties (double click on this Properties section).
You can delete this file to test again.
I have this code
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Registry.CurrentUser.OpenSubKey(#"HKEY_CURRENT_USER\software\google");
}
}
The form1 has only ONE Button .
I want to open **HKEY_CURRENT_USER\software\google** by click the button .
But the Button do nothing after clicking , why ?
Your code opens an object and returns it. If you assign this object to variable like this:
var key = Registry.CurrentUser.OpenSubKey(#"HKEY_CURRENT_USER\software\google"); you can for example change it after that assignment.
If you want to open regedit tool you should use following code: Process.Start("regedit.exe")
In programming in general there are two types of functions:
Functions with side effects which actually do something, and functions without side effects which return something.
In your case OpenSubKey returns a RegistryKey. You have to do something with it, otherwise your button press has no effect.
And the reason is, that your function call has no side effects.
There are 2 issues with your code.
1) even if you do something with the value that you retrieve from the function, it will not work for a simple reason:
when you do:
Registry.CurrentUser.OpenSubKey(#"HKEY_CURRENT_USER\software\google");
you are opening a sub key of the current user key, and you are seaching for another current user within the current user. meaning that you are actually searching for:
"HKEY_CURRENT_USER\HKEY_CURRENT_USER\software\google"
what you need to do is search for #"software\google"
eg:
MessageBox.Show(Registry.CurrentUser.OpenSubKey(#"software\google").ToString());
2) you are misunderstanding the meaning of "open". open doesnt mean that it will open the registery editor window, it just means that behind the scenes, your app will open the key for further use.
if i understand you correctly, you want the registery editor window to open up, and navigate automatically to the Google key. for that, you can do the following trick:
set the regedit's LastKey key to the Google path, and then once you open regedit it will navigate there automatically:
RegistryKey myKey = Registry.CurrentUser.OpenSubKey(#"Software\Microsoft\Windows\CurrentVersion\Applets\Regedit", true);
if (myKey != null)
{
myKey.SetValue("LastKey", #"HKEY_CURRENT_USER\software\google", RegistryValueKind.String);
myKey.Close();
}
System.Diagnostics.Process.Start("regedit.exe");
I am having this problem with loading/saving application settings. I am saving the settings like this
string value = "test";
Properties.Settings.Default.test1 = value;
Properties.Settings.Default.Save();
It works if I load the settings while the program is still running but whenever I close the application and start it up again the saved value is gone.
string savedValue = Properties.Settings.Default.test1;
I have tried Refreshing after saving but that does not seem to help.
However I found where the settings are stored (Appdata\Local) and the value indeed shows up in there but it does not get loaded.
This is how it is done. If your code doesn't work, try creating an MCVE to enable further debugging.
namespace ConsoleApplication5
{
using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("This should be blank on first run, but not on subsequent: {0}", Properties.Settings.Default.test1);
string value = "test";
Properties.Settings.Default.test1 = value;
Properties.Settings.Default.Save();
}
}
}
Pass #1:
This should be blank on first run, but not on subsequent:
Press any key to continue . . .
Pass #2:
This should be blank on first run, but not on subsequent: test
Press any key to continue . . .
I am a WPF Developer , so am sure that the Properties.Settings definitely save while use
Properties.Settings.Default.Save();
Or
Properties.Settings settings= new Properties.Settings();
settings.YourParameter=true;
settings.Save();
If you don't get the updated value in settings then you first use
settings=new Properties.Settings();
You will be definitely get new result.
I'm a beginner in winforms, and just starting using it's preferences.
So, I add in my Settings.settings a Value named path, as string and User Scope.
I change it when I choose a new path with a FolderBrowserDialog and then, after a click on a Ok button, i change the preferences like this :
private void buttonPref_Click(object sender, EventArgs e)
{
Form2 subForm2 = new Form2(textBoxRep.Text);
subForm2.ShowDialog();
if (subForm2.DialogResult == DialogResult.OK)
{
Settings.Default.path= subForm2.rep();
subForm2.Close();
}
else
{
subForm2.Close();
}
}
public string rep()
{
return textBoxRep.Text;
}
Then, when I run my Application, I load the value in my preferences :
textBoxRep.Text = Settings.Default.path;
But, the value is set to empty after every new run.
So I tried with a Application Scope, but I got a read Only error on this : Settings.Default.path
How can I fix this? Is there a way to register the settings after mofified them?
Thank you.
you need to call Save method as below
Settings.Default.path= subForm2.rep();
Settings.Default.Save();
Settings that are application-scoped are read-only, and can only be
changed at design time or by altering the .config file in between
application sessions. Settings that are user-scoped, however, can be
written at run time just as you would change any property value. The
new value persists for the duration of the application session. You
can persist the changes to the settings between application sessions
by calling the Save method.
How To: Write User Settings at Run Time with C#
You need to also call Settings.Default.Save();
I have to pretty simple pieces of code:
private void frmMain_Load(object sender, EventArgs e)
{
string[] EmployeeNames = Settings.Default.Employee_Names.Split(new char[] {';'}, StringSplitOptions.RemoveEmptyEntries);
employee_NameComboBox.Items.AddRange(EmployeeNames);
}
And
private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
{
string Employee_Names = string.Empty;
foreach (string Name in employee_NameComboBox.Items)
{
Employee_Names += Name + ";";
}
if (Employee_Names.Length > 1)
{
Settings.Default.Employee_Names = Employee_Names.Substring(0, Employee_Names.Length - 1);
Settings.Default.Save();
}
}
So, to test my code I added the values "Foo;Bar"
To my Settings.Settings in the VS Designer
but when I run the Code Nothing comes up. So I add this code:
private void AddName()
{
if (!employee_NameComboBox.Items.Contains(employee_NameComboBox.Text))
{
employee_NameComboBox.Items.Add(employee_NameComboBox.Text);
}
}
And then I call it on the Leave event of employee_NameComboBox:
private void employee_NameComboBox_Leave(object sender, EventArgs e)
{
AddName();
}
To test this I enter the employee_NameComboBox and type in "Employee1" and leave the CB then enter again and type in "Employee2" and Leave now when I click on the drop down I have these strings in the Items Collection. Great!
But when I close the application and then goto my settings.settings under properties in the Solution Explorer the values "Foo;Bar" are still there.
Continue to start Debugging again and when I click the drop down, the values are "Employee1" and "Employee2". In turn I search through the Solutions Directory in Windows Explorer to try and find the new settings file and I simply CAN NOT find ANY files with "Employee1" or "Employee2" all I see in ANY files is "Foo;Bar".
How can this be? I am saving to somewhere but where!
Also Why doesn't the debugger use the values in the settings.settings or App.Config files?!?!?!
Note: I also tried doing a Find from Visual Studios and Searched Entire Solution for the value Employee1 to no avail.
My end result is that I would like to send out an application with editable combobox that can be saved and edited without a Re-compile.
When such setting are changed at run-time, the values are stored in the following directory:
C:\Documents and Settings\<username>\Local Settings\Application Data
That's where the app then goes and looks for them the next time.
The settings are overwritten on each build by whatever you have set up in VS. If you want to use the settings from a previous compilation (or once deployed to a client machine, a previous version after an update) you have to use Settings.Upgrade(). This should be called once per runtime, most easily in the constructor. See this other StackOverflow question. C# .NET Application Settings and Upgrading