2

Can there be any locking issues when there is a single app.config file for several applications?

config = ConfigurationManager.OpenExeConfiguration(Path.Combine("C:\test\root", "Master.exe"));
string logpath = config.AppSettings.Settings["Log.Path"].Value;
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
Akshay J
  • 5,362
  • 13
  • 68
  • 105

2 Answers2

1

App.configs are read only and shouldn't change unless the application publisher publishes a new version of it. User configs setting are stored in a user.config file not in the app.config. In the abscence of this user.config file the app.config provides the default value.

I commented in your last question 5 minutes ago Common app.config for multiple applications

Since the files should be marked read only external apps should not lock the files. If you are writing to the app.configs from different assemblies the config file might get locked. I wouldn't recommend writing to the App.Config from different assemblies but if you do, in the read/write operations put in a Try/Catch and if its an IO Exception check if the file is locked:

private static bool IsFileLocked(Exception exception)
{
    int errorCode = Marshal.GetHRForException(exception) & ((1 << 16) - 1);
    return errorCode == 32 || errorCode == 33;
}
Community
  • 1
  • 1
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
0

You're doing something a tad unconventional in having multiple apps share an app.config. As its name implies, app.config is intended to be per app. There are methods in System.Configuration that allow an app to write to the app.config (thus write-locking it for a moment. Another app trying to write at the same moment will receive an error.) I personally tend to put shared config in the database.

Yet if you have a strong case for this, you should be OK. Apps reads app.config upon startup and cache the settings. I'd suggest making a documented convention among your developers that apps sharing config shouldn't write to the app.config.

Note that ASP.NET web.config has some extra behavior in that it gets monitored for changes. But I think this is specific to IIS; and I don't think you're dealing with web.config.

Dave Clausen
  • 1,302
  • 1
  • 12
  • 23