How do I compile my App.config into my exe in a VS2010 C# console app?

前端 未结 10 1921
傲寒
傲寒 2020-12-05 17:24

I\'m creating a console app in Visual Studio 2010 with c#. I want this app to be stand alone, in that all you need is the exe, and you can run it from anywhere. I also want

相关标签:
10条回答
  • 2020-12-05 18:08

    IL Merge has lot issue with wpf executable also slow. I ended up using Cosura.Fody https://github.com/Fody/Costura and using command line parameter for passing my app config value. Also using iexpress http://en.wikipedia.org/wiki/IExpress to create final executable with command line args and exe merged together

    0 讨论(0)
  • 2020-12-05 18:14

    As others have pointed out, the idea behind a configuration file is to avoid hard-coded values.

    What you might do as an alternative is to to write a custom configuration section, with every element optional and with default values. That way anyone who can get by with the defaults doesn't need a config file. But if they need to override a default, they can provide one.

    (Sorry, just a bit of brainstorming. I don't have an example available.)

    0 讨论(0)
  • 2020-12-05 18:16

    Take a winform application for example, during compilation, a file 'xxx.EXE.config' will be generated along with the output EXE file. It will contain the 'app.config' settings. Distribute this as well.

    0 讨论(0)
  • 2020-12-05 18:20

    Best workaround looks like to create it yourself at the application startup.

    1. Add App.Config as a resource, rename it to "App_Config"
    2. Check if config file exists
    3. If not, write default .config file

    Example code:

    Program.cs

        [STAThread]
        static void Main()
        {
            CreateConfigIfNotExists();
        }
    
        private static void CreateConfigIfNotExists()
        {
            string configFile = string.Format("{0}.config", Application.ExecutablePath);
    
            if (!File.Exists(configFile))
            {
                File.WriteAllText(configFile, Resources.App_Config);
            }
        }
    

    Remember that it will only write your current config when building. It will not update it automatically when you deploy a new version. It will include the config as is when building. But this could be enough :)

    0 讨论(0)
提交回复
热议问题