When I change a field in an web or app.config of a C# project, would that value automatically feed into the program without any restarts or interruptions in the program? Is the program always fetching from the config files every time that field is requested or is it cached by the program somewhere. How does this work?
I want a situation where I would change the value in the config, and want that value automatically pulled by the application like instantly. Changes, and program pulls that value instantly.
ASP.NET monitors web.config file and will recycle AppDomain when it notice changes. It will wait for the current requests to be processed and will queue any new requests coming.
So yes, the changes will be pulled be the application, but not instantly and not without interruption (although that depends on your definition of 'instantly').
You are supposed to change the web.config through code, as it results in restart of AppDomain. You should make a new xml file for setting and change it through code.
Related
I'm currently facing a following issue,
I use xml file to store information and once my application is started it pulls data from xml file and based on that dynamically constructs views. The problem is that users can update data in xml file within the application and save it. However as views are already loaded with all data, user now doesn't see new data until application is restarted (closed and started again).
I'm looking for a way to refresh data inside certain elements after xml file is updated, or make it so it constantly checks data in xml file every time.
I'm thinking some sort of method that does this on a "save" button click when users update xml.
It looks like you are going to want something like the FileSystemWatcher.Changed Event. You can hook up to the event when the application loads, and know when the file changes. This will allow you to catch changes made both internally (by your program) and externally (by a text editor or something like that).
When a file change is detected, simply load the new file and refresh the views with the new data. This part can be a little complicated depending on how the data is loaded into the views. This could also require detection of changes in the views when external changes have also been made. Probably best to prompt the user before reloading. This all depends on your use case and the expected use by the user.
Sauce: http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.changed.aspx
I was just wondering if anyone has any input on how to save a C# Winform setting?
Currently, I have a form that has various radio buttons, directory browsers, date pickers etc. I was wondering what is the best strategy to save these settings to an external file that can be loaded at a later date. So essentially each configuration can be loaded, executed, and then another configuration loaded. Also, the configuration can be passed across installations / users.
Application-scope settings are read only, and can only be changed at design time or by altering the .exe.config file in between application sessions. User-scope settings, 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 changes to user settings between application sessions by calling the Settings.Save method. These settings are saved in the User.config file.
Write and Persist User Settings at Run Time
Access the user setting and assign it a new value, as shown in the following example:
Properties.Settings.Default.myColor = Color.UserGreen;
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();
I solved this problem with a class or struct which contains all settings. My form-class had a constructor which accepted such a setting-instance.
This settings-class/-struct was implementing ISerializable. So you can save it easily into files and load it from.
This is by far not the best way to do it, but it is quiet easy to implement.
I have a program that overwrites a certain set of files required for my website, however the traffic on my website has increased so much that i now get the error File in use. Which results in it being unable to update the file..
This program runs every 5 minutes to update the specified files.
The reason I let this program handle the writing of the file and not the program, is cause I also need to update it to a different webserver (through ftp). this way I also ensure that the file gets updated every 5 minutes, instead of when a user would take a look at page.
My question therefore is; Can i tell IIS7.5 to cache the file after (say 5 seconds to 1 minute) it has been updated? This should ensure that the next time the program runs to update the file it won't encounter any problems.
Simplest solution would be change that program that refreshes the file to store that new information in database, not in filesystem.
But if you can't use database I would take different approach, store file contents to System.Web.Caching.Cache with time when was last modified, and then check if file is changed if not use cached version, and if was changed store new contents and time in same cache variable.
Of course you will have to check if you can read the file, and only then refresh cache contents, and if you can not read the file you can simply get last version from the cache.
Initial reading of file would have to be in application_start to ensure that cache has been initialized, and there you will have to wait until you can read file to store it in the cache for the first time.
Best way to check that you can read from file is to catch exception, because lock can happen after your check, see this post : How to check for file lock?
I have a web app that writes data to a file every interval, say 10 minutes.
I have another console app that reads the file and does something with data.
The problem is: sometimes the file is staled, in use by the web app while the console app is trying to copy it. Which causes error, lock error.
I have two questions, How can i force the file to closed from another program, not on the same machine?
Other question related to web app, say, there s no request coming to a server for a while, file is never closed. how can i ensure that after an interval, file is closed? background timer thread?
EDIT: OS is window 2003 server.
any suggestions?
If your program "doing something with the data" doesn't actually modify the file then you can open it read-only instead and avoid the whole locking situation. Do you need write access?
You can keep track of your sessions by writing a session count function. When the number of sessions reaches zero you can then close your file. It would be helpful if you gave us more information about what exactly you are trying to do so we know the program's flow.
Use Session_Start and Session_OnEnd to keep track of sessions. This site has a good article on ways to keep track of sessions.
I've been trying to modify my application to deploy and update using ClickOnce. I've managed to get the program working but I'm having trouble with the program configuration. My program uses a custom XML configuration file located in the application directory. This raises 2 major problems.
1.) The configuration file is very hard to get to. Without knowledge of how ClickOnce works the user will not be able to locate it.
2.) Currently if I change the configuration file ClickOnce automatically "updates" the configuration file to the original version, destroying my configuration.
Ideally I would like it to move the configuration file to another location and create a start menu shortcut to it next to my application. But if I change the program to do this can I still deploy the application using ClickOnce?
Thanks in advance,
Fr33dan
Why don't you put a copy of the configuration in the users app data folder (this can be done on first run) - then have a button in your application which opens it (either externally or in your application)?
You can always store your configuration data in the Application Settings. This won't get overwritten on every ClickOnce change or update (unless you change the Type of the setting). You can then create a simple form to update it. That's the technique many .NET developers use for screensavers.
There are a number of things you can do here to mitigate this as a problem.
Firstly, using what's already there - the configuration data has two parts (excuse me as I'm working from memory) app config and user config. The app config is basically defined when the app is pulled down however the user config is just that - you set up the defaults and then, once set by the application on behalf of the user, it won't be overwritten when the app is updated.
It should be straightforward enough to provide a configuration editor - something as simple as a two column grid would be sufficient with a read only label column and an editable value column (although you're going to be somewhat challenged on validation).
Alternatively, if you're happier with a more traditional configuration, then you need precisely 1 user value and that would be the location for the config file... if you don't know if (or can't find the file) prompt to create, dump your default config to the specified location from a resource within you app and then you've got your config file and away you go.
One project I worked on, we made the app download a configuration file from the server it was deployed from (this was done on each startup to cope with if app was added to the Start Menu and cached). The ClickOnce API gives you the server address.
On another project we just pass a few config values as query strings to the ClickOnce app, these were generated by the Asp.net page that had the link to the app.
This allowed customers to change the config for their site without having to resign etc.
(This does not help with per-user config)