Question

I have an application that is running .Net 3.5 and want to upgrade all of the users to V4.0 using a setup kit. The upgrade works fine, except that I don't want to hand-edit all of their configuration files to add the required line:

<startup useLegacyV2RuntimeActivationPolicy="true"><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup>

That is required to make Crystal Reports 2010 work correctly. I was hoping that the Configuration namespace had a section labelled startup, but no such luck. I am unable to add a new Section without having a section type defined. Does anyone have any experience with doing this using the Configuration class?

Thanks, Neil

Was it helpful?

Solution

The most 'elegant' way I've found doing that is using some raw xml, which includes the entire startup section. So while it might not be the most 'correct' way, it is still better than manual edit of the config file IMO.

Here is a working code example for editing the config file from within the same executable (you might need to provide the path in OpenExeConfiguration to do so from your installer):

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var sec = config.Sections["startup"];
sec.SectionInformation.SetRawXml("<startup useLegacyV2RuntimeActivationPolicy=\"true\"><supportedRuntime version=\"v4.0\" sku=\".NETFramework,Version=v4.0\" /></startup>");
config.Save(ConfigurationSaveMode.Modified);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top