0
votes

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, }

2
can you please clarify what is your intention behind publishing ?Clint
After "publish" was installed and installed on other computers, running the software, I got the error "Loading configuration file wrong".Huanhuanquan Nb
So I understand, you have created your wpf app and I assume it works on its own and then you right click and publish. And what are you installing on other computers ?Clint

2 Answers

1
votes

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.

0
votes

A configuration file should probably not be statically defined at compile-time, otherwise it defeats its purpose of being editable later without rebuild.

Your application should survive start-up without a valid configuration file, because this file will be edited by end-users and thus contain errors.

If you only need a static configuration file, you could change your configuration file "Build Action" to "Embedded Resource" and load it using Assembly.GetExecutingAssembly().GetManifestResourceStream() method. That would mean they would no longer be editable after build though.

You can also have your code create the configuration file on start-up if it doesn't exist and have it filled with default values (for example, by using default values loaded from an embedded configuration file).