locked
app.config on other location than the location of .exe RRS feed

  • Question

  • I have a WPF application which is by default installed in programs files folder.

    At runtime, app.config file is modified and saved. Based on this modifications, enterprise library logs application logging.

    But on windows 10 machine, modifications of app.config files are not saved due to security issues.

    I tried below option but enterprise library continue to read app.config from original location.

    AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE",  _tempFolderPath + "\\DMSShell.exe.config");

    Any suggestions?


    • Edited by aariefe Thursday, February 25, 2016 12:06 PM
    • Moved by 宝宝徐 Friday, February 26, 2016 5:49 AM
    Thursday, February 25, 2016 12:06 PM

Answers

  • >>At runtime, app.config file is modified and saved. Based on this modifications, enterprise library logs application logging.

    This sounds like a bad idea in the first place. Why are you modifying the configuration file at runtime? You could store the 'dynamic' settings somewhere else, like for example in a a database or a simple text file in your output directory and retrieve them from there instead of trying to dynamically modify the App.config. This is my first suggestion.

    >>I tried below option but enterprise library continue to read app.config from original location.

    If you want to read a custom configuration file, you could use the ConfigurationManager.OpenExeConfiguration method instead of using ConfigurationManager directly:

    Configuration config = ConfigurationManager.OpenExeConfiguration(@"C:\Path\WpfApplication1.exe"); //! this looks for a file called WpfApplication1.exe.config

    Note that method looks for a config file called WpfApplication1.exe.config even if you pass in "WpfApplication1.exe". Also note that an executable (.exe) must exist in the folder. Please refer to the following thread for more information: http://stackoverflow.com/questions/75978/accessing-app-config-in-a-location-different-from-the-binary

    This approach is not much different from simply accessing a simple text file containing the settings though.

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    Saturday, February 27, 2016 10:22 AM

All replies

  • Hi aariefe,

    Thank you for posting in MSDN forum.

    This forum is discuss .NET Framework Setup and Servicing.

    As your issue is related to WPF ,I help you move this case to Windows Presentation Foundation (WPF)  forum for dedicated support.

    Thank you for your understanding.

    Best Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.

    Click HERE to participate the survey.


    • Edited by 宝宝徐 Friday, February 26, 2016 5:48 AM
    Friday, February 26, 2016 5:48 AM
  • You can't save to folders within program files since win 7.

    You can store files in appdata:

    http://social.technet.microsoft.com/wiki/contents/articles/30915.c-local-files.aspx

    If all you want is to save some specific settings then I would consider just putting an xml file in appdata and reading that instead.

    You could use a custom settings provider or just regular xml reading and set properties.

    If that's no good to you then you can make the entire application use a config file elsewhere using the approach here:

    http://stackoverflow.com/questions/13584692/load-a-wpf-app-config-myapp-exe-config-from-a-location-different-from-the-app

    The markup from there:

    Application x:Class="InstallTool.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    StartupUri="Window1.xaml"
    Startup="AppStartup"

    Code:

        public void AppStartup(object sender, StartupEventArgs e) {
            if (AppDomain.CurrentDomain.IsDefaultAppDomain()) {
                string appName = AppDomain.CurrentDomain.FriendlyName;
                var currentAssembly = Assembly.GetExecutingAssembly();
    
                // Setup path to application config file in ./Config dir:
                AppDomainSetup setup = new AppDomainSetup();
                setup.ApplicationBase = System.Environment.CurrentDirectory;
                setup.ConfigurationFile = setup.ApplicationBase + 
                                    string.Format("\\Config\\{0}.config", appName);
    
                // Create a new app domain using setup with new config file path:
                AppDomain newDomain = AppDomain.CreateDomain("NewAppDomain", null, setup);
                int ret = newDomain.ExecuteAssemblyByName(currentAssembly.FullName, e.Args);
                // Above causes recusive call to this method.
    
                //--------------------------------------------------------------------------//
    
                AppDomain.Unload(newDomain);
                Environment.ExitCode = ret;
                // We get here when the new app domain we created is shutdown.  Shutdown the 
                // original default app domain (to avoid running app again there):
                // We could use Shutdown(0) but we have to remove the main window uri from xaml
                // and then set it for new app domain (above execute command) using:
                // StartupUri = new Uri("Window1.xaml", UriKind.Relative);
                Environment.Exit(0);
                return;
            }
        }


    Hope that helps.

    Technet articles: WPF: Layout Lab; All my Technet Articles

    Friday, February 26, 2016 9:45 AM
  • >>At runtime, app.config file is modified and saved. Based on this modifications, enterprise library logs application logging.

    This sounds like a bad idea in the first place. Why are you modifying the configuration file at runtime? You could store the 'dynamic' settings somewhere else, like for example in a a database or a simple text file in your output directory and retrieve them from there instead of trying to dynamically modify the App.config. This is my first suggestion.

    >>I tried below option but enterprise library continue to read app.config from original location.

    If you want to read a custom configuration file, you could use the ConfigurationManager.OpenExeConfiguration method instead of using ConfigurationManager directly:

    Configuration config = ConfigurationManager.OpenExeConfiguration(@"C:\Path\WpfApplication1.exe"); //! this looks for a file called WpfApplication1.exe.config

    Note that method looks for a config file called WpfApplication1.exe.config even if you pass in "WpfApplication1.exe". Also note that an executable (.exe) must exist in the folder. Please refer to the following thread for more information: http://stackoverflow.com/questions/75978/accessing-app-config-in-a-location-different-from-the-binary

    This approach is not much different from simply accessing a simple text file containing the settings though.

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    Saturday, February 27, 2016 10:22 AM