I have put 2 strings in the built-in resource file of my main project (not sure how I define this properly). How can I access these values? How would I change them? The resource is public so that the user can change them.
eg:
Project1.Resources.Get("string1").Value();
and
Project1.Resources.Set("string1") = "whatever";
This is pseudo code.
Resources are not intended to be changed at runtime. You should consider using user settings instead. For details, see Using Application Settings and User Settings on MSDN.
This will allow you to use the designer to build the settings, and write:
string string1 = Properties.Settings.Default.String1;
And:
Properties.Settings.Default.String1 = "whatever";
Properties.Settings.Default.Save();
You can get resource by writing the following piece of code
Project1.Resources.String1
You can not change it at runtime.
Related
I am building an application, which has a form where the user can configure all his settings. When the application is loaded, the previously configured settings should reflect to the GUI (The UI should be consistent to the saved settings).
What I am currently doing is creating the settings on the project properties and I have a LoadSettings() method, which gets the values and outputs them to each component on the UI.
The thing is that this is getting VERY messy, and I don't like it at all.
So, that got me wondering, what are the correct approaches to achieve what I want, but yet getting high quality code? Any patterns for that?
private void LoadConfigs()
{
checkBoxStartOnStartup.Checked = ExistKeyValue(#"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", "Wallbase Downloader");
checkBoxCheckWallbaseOnline.Checked = Settings.Default.CheckWallbaseOnlineStartup;
comboBoxResolution.SelectedIndex = comboBoxResolution.FindStringExact(Settings.Default.Resolution == string.Empty
? GetScreenResolution()
: Settings.Default.Resolution);
comboBoxCondition.SelectedIndex = Settings.Default.ConditionIndex;
textBoxWallpaperFolders.Text = Settings.Default.WallpaperFolder;
numericChangeInterval.Text = Convert.ToString(Settings.Default.ChangeIntervalValue);
comboBoxChangeInterval.SelectedIndex = Settings.Default.ChangeIntervalIndex;
numericCheckInterval.Text = Convert.ToString(Settings.Default.CheckIntervalValue);
comboBoxCheckInterval.SelectedIndex = Settings.Default.CheckIntervalIndex;
numericWallpapersToLookFor.Text = Settings.Default.WallpapersToLookFor.ToString();
}
Well, WinForms are not the cleanest framework around...
What you could do is to load all settings when your application starts up and store them in some storage that is available to all forms, e.g. in a static property in a helper settings class.
You can then access that static property from each form when it loads and make all necessary changes to the form based on the settings.
You can use a Hashtable and use English strings for key to make your code really readable. Then serialize it to file on exit and deserialize it back when application loads. Save the serialized file to some common location so that you do not lose it.
how would I edit a string in the resources of my project? I get this error when I try it:
Property or indexer 'Project.Properties.Resources.ExternalIp' cannot be assigned to -- it is read only
This is what I do:
Resources.ExternalIp = utf8.GetString(webClient.DownloadData("http://automation.whatismyip.com/n09230945.asp"));
Properties.Ressources are readonly ("compiled"), you have to use Properties.Settings & put the Scope to "User" so it will be 'ReadWrite'
Project.Properties.Settings.Default.ExternalIp = utf8.GetString(webClient.DownloadData("http://automation.whatismyip.com/n09230945.asp"));
Project.Properties.Settings.Default.Save();
Resources are not supposed to be written to; they're embedded in the executable, so changing them would involve modifying the executable.
From your code, it looks like you actually need application settings, not resources.
A settings file is a nice way to access important data anywhere in a program since we can simply say
string dogType = Settings.Default.FavoriteDogType; or Settings.Default.FavoriteDogType = "Chiwawa";
I like this but the file is managed by the computer (stored in user settings). For my application, I would like to allow the user to select there own settings file (stored by them) and use this file for the settings as in
NewWayOfUsingSettings.Load("bobsFavoriteDogInfo.xml");
and then being able to call
string dogType = NewWayOfUsingSettings.FavoriteDogType;
anywhere in the program where "bobsFavoriteDogInfo.xml" would contain the settings to use. (alternatively, Jane could load "JanesBestDog.xml" etc.) Does C# support a simple way to do this?
Thanks!
So I am using C# ASP.NET 3.5 and I would like to add a feature to my site to turn on and off a sort of debug mode for testing purposes.
Is there a best way to have a file or class that stores or returns simply if myDebug is on or off. It has to be accessed fast since it will be used a lot on multiple pages and it should be easy to set using the website itself.
My first thought is just a class with get/set which is stored on every page... perhaps the master page?
Thanks for any input
-Scott
Sounds like something you'd want to put in AppSettings in your web.config.
(I'm assuming that setting compilation debug to true in web.config is insufficient for what you're trying to do.)
Use AppSettings.
You can get your app settings like so:
string appSettingValue = ConfigurationManager.AppSettings["key"];
You can change your app settings like so.
Code below has been copied from blog link above to show as sample:
private void ChangeAppSettings(string key, string NewValue)
{
Configuration cfg;
cfg = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
KeyValueConfigurationElement setting = (KeyValueConfigurationElement)cfg.AppSettings.Settings(key);
if ((setting != null)) {
setting.Value = NewValue;
cfg.Save();
}
}
You might have to call ConfigurationManager.RefreshSection("appSettings"); after the save to have the app see the changes.... Im not sure if the cfg.Save() will reload/refresh the settings.
Here's my code:
public void VerifyIfFirstTimeRun()
{
if (System.Configuration.ConfigurationSettings.AppSettings["FirstTimeRunning"] == "true")
{
//Do bla bla bla
//Then do bla bla bla
System.Configuration.ConfigurationSettings.AppSettings["FirstTimeRunning"] = "false";
}
}
Problem is, I'm testing this with the F5 key, it boots up and sure enough using a breakpoint shows that it is indeed going inside the If Condition, but when I "Stop" the application and press F5 again it goes inside the If Condition AGAIN. Is this standard operational procedures?
If so, how can I test if its working?
This is going against the spirit of what the App.config file is used for ... but to answer your question, you need to do System.Configuration.Configuration.Save().
Edit:
App.config is typically used to configure database connections, providers, etc. And is usually set once at installation. It's suggested that user settings go into a separate config file user.config. See this for the explanation.
Edit:
System.Configuration.Configuration class.
Note - now that I read why you're using these settings, may I suggest another way? You should probably just check if the file is there:
if (!File.Exists("thefilepath"))
{
AlertUserOfMissingFile();
ChooseNewFile();
}
It's safer this way anyhow because having a setting set to true doesn't necessarily mean the file is where you think it is.
I'm not sure you should expect this to save; you can, however, have a settings file that has a setting (a bool in this case) in the user's context, which saves (when you ask it to) via Settings.Default.Save().
I don't advice you to use App.settings for this purpose.
Take a look a this article
Settings in C#
In Solution Explorer, expand the Properties node of your project.
In Solution Explorer, double-click the .settings file in which you want to add a new setting. The default name for this file is Settings.settings.
In the Settings designer, set the Name, Type, Scope, and Value for your setting. Each row represents a single setting. Figure 1 shows an example of the Settings designer.
If you want to persist changes to user settings between application sessions, call the Save method, as shown in the following code:
Properties.Settings.Default.Save();