Reputation: 144
I am working with a legacy WinForms application, upon opening the main form the designer crashes and spits out the error "Method 'System.Configuration.ConfigurationManager.AppSettings' not found.". The application however runs fine and loads as expected, I have done work on this application within the past two weeks and it has been opening in the designer as expected.
From looking at other questions around this area the obvious answer is usually because the op forgot to add a reference to System.Configuration. I have made sure this is referenced in the project.
I tried nuking my entire VS / .Net installation by removing everything and reinstalling, this did nothing.
The only code referencing the System.Configuration namespace is the below code, commenting it out allows the designer to load normally but with this code added in it fails.
Me.Text = "Application Env - " + System.Configuration.ConfigurationManager.AppSettings("test")
Below is the contents of my app.config
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="test" value="test"/>
</appSettings>
</configuration>
The error seems to hint at not being able to locate the assembly for System.Configuration but intellisense picks up the full path, and I can see it in the dll hint path's folder.
Upvotes: 1
Views: 257
Reputation: 125217
The solution which is shared in the other answer by TnTinMn is right, but the reason of the error is not accurate. In fact, visual studio designer cannot resolve the indexer but can process the method.
So assuming you have a reference to System.Configuration, technically it's enough to replace the indexer by the following method call:
Me.Text = "Application Env - " + System.Configuration.ConfigurationManager.AppSettings.Get("test")
But while it prevent the designer error and while it will show expected result at run-time, but you should never use above code, because:
Me.Text = "Application Env - "
System.Configuration.ConfigurationManager.AppSettings
will try to read Visual Studio application settings and not your application settings.Another alternative solution, is using application settings property binding.
Upvotes: 1
Reputation: 11801
The only code referencing the System.Configuration namespace is the below code, commenting it out allows the designer to load normally but with this code added in it fails.
The WinForm Designer can not load. That implies that you edited the problem line within the InitializeComponent
method to include the AppSettings
call.
The Visual Studio Winform designer code does not reference System.Configuration
and as such can not process that line. You indicated that this is a legacy project. If the project is pre-VS2008, the initialize component method may still be in the FormName.vb file. VS2008 introduced partial classes, and places the designer code in the FormName.designer.vb.
Move the offending code line to code that executes only in the run-time environment. For example:
Protected Overrides Sub OnLoad(e As EventArgs)
MyBase.OnLoad(e)
Me.Text = "Application Env - " & System.Configuration.ConfigurationManager.AppSettings("test")
End Sub
or add a constructor to the form and place it there.
Public Sub New()
InitializeComponent()
Me.Text = "Application Env - " & System.Configuration.ConfigurationManager.AppSettings("test")
End Sub
Upvotes: 1