Search code examples
c#wpfconfigpublish

publish the C# project with configuration file


I want to publish the C# project,Visual Studio 2019 ,WPF.

First, application publish; Second, install the application; Third, a window pops up after the application installation is complete, showing an error: "An error occurred while loading the configuration file"

I want to put the configuration file in the publish application.

[Code in Visual Studio 2019 WPF][2]

(MainWindow.cs)

private void MainWindow_OnContentRendered(object sender, EventArgs e) { // loading the configuration file try { Config = RimageConfig.LoadFromFile("config.json"); } catch { ShowError**("An error occurred while loading the configuration file", Close);** return; }

(config.json) { "Rk": 0.3167,// "Rb": -1.6349, "Rg": 8.6982, "Rt": 200, //"CLeft": 655, //655 "CLeft": 520, //655 "CTop": 120, "CWidth": 2350, "CHeight": 2300, "Pa": 100, "Pb": 30, //"Pa": 255, //"Pb": 0, }


Solution

  • Try setting "Copy to Output Directory" property to either "Copy always" or "Copy if newer". To do that in Visual Studio 2019:

    • Go to solution explorer
    • Navigate to your config file
    • Right-click on it
    • Properties
    • Select desired "Copy to Output Directory" value

    This should ensure, that when you use Publish method configs will be copied to publishing directory.

    EDIT: As Corentin pointed out, in my original answer I missed the fact that you are using ClickOnce. In that case you have to go to file properties and set "Build Action" to Content. Then go to Project -> Properties -> Publish, click on Application Files... and make sure, that Publish Status and Download Group for you configs are set to Include and (Required) respectively.