سؤال

I joined a project where .NET Windows Forms application is using Settings mechanism (Project > Properties > Settings) to preserve both user settings and application settings. I have created command-line tool which uses LoadAssembly(<main application EXE file>) to perform some functionalities of main app. Upon invoking of main app's methods everything works fine except when comes to Settings – of course, because they were not loaded – settings loader was skipped.

But the application heavily relies on settings so I need to call that settings loader explicitly. But I can't find it: I have analyzed all files of blank .NET project with Settings added and loading routine is nowhere to find.

I think at worst I can workaround the problem and implement

  • locating settings file
  • explicit loading of its content

but I do not like re-inventing the wheel and especially I don't like non-transparent handling of loading of settings on .NET platform.
Is there any way to invoke built-in code used by .NET framework to load My.Settings on application start-up?

Note: Application is written in VB.NET but I think for C# the thing is the same, so do not hesitate to present C# way of thinking, if you want to.

هل كانت مفيدة؟

المحلول

I have recently found, that default way how settings are handled does not allow nor even support my scenario. Evaluation of

ConfigurationManager.OpenExeConfiguration( ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath

gives different local settings directory when invoked from original application and when invoked from loaded assembly. Compare outputs of the same method:

When called directly:

C:\Users\Miroxlav\AppData\Local\WindowsApplication1\WindowsApplicationSetting_Url_za1afclumj0mqqjsghdpysdumjr5jd21\1.0.0.0\user.config

When called as part of loaded assembly invoked from another app:

C:\Users\Miroxlav\AppData\Local\WindowsApplication1\TestWindowsApplicationSet_Url_cxikfslvgbja50yw4lvnvugko41ou5jz\1.0.0.0\user.config

So with default settings provider even location of config file cannot be determined directly. This basically renders void all potential subsequent steps of direct retrieval of My.Settings of loaded assembly (without helper files, additional workarounds utilizing fixed settings etc. – while keeping default settings provider)

The way out can be to write custom settings provider which stores settings at universally accessible location although it is much greater overhead than I expected I will find.

Update: As a quick workaround I have implemented helper file to save config location on start of the main application. When main application's code is launched as loaded assembly, I'm restoring My.Settings from the config file found at location saved in helper file.

نصائح أخرى

System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration (ConfigurationUserLevel.None);

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top