Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# properties/settings not defined after changing project name and namespace

I am updating an old application built several years ago and the global namespace and, project.cs file, and the main form were all named as email2case_winForm

I wanted to change this (alongside rebuilding the application in more of an OOP way to make it more flexible, and to be able to re-use code), so changed the names to email2case

But now I am having trouble compiling because when reading the settings and properties of the project, those members cannot be found.

Here is an example of the code I am trying to use to read the properties.Settings:

XElement x = XElement.Load(email2case.Properties.Settings.Default.e2cConfigFile);

and the compiler error for that one is:

'email2case.email2case' does not contain a definition for 'Properties'

Here is how a class is reading from the global settings:

this.Url = global::email2case.Settings.Default.email2case_sforce_SforceService;

and the compiler error for that one is:

The type or namespace name 'Settings' does not exist in the namespace 'email2case' (are you missing an assembly reference?)

I have a hunch that the problem is caused by the definitions in app.Config and here it is:

<configuration>
    <configSections>
        <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
            <section name="email2case.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
            <section name="email2case.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
        </sectionGroup>
        <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="email2case.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <applicationSettings>
      <email2case.Properties.Settings>
        <setting name="email2case_sforce_SforceService" serializeAs="String">
          <value>https://login.salesforce.com/servi...</value>
        </setting>
        <setting name="e2cConfigFile" serializeAs="String">
          <value>C:\email2case\data\e2cConfig.xml</value>
        </setting>
      </email2case.Properties.Settings>
      <email2case.Properties.Settings>
        <setting name="email2case_sforce_SforceService" serializeAs="String">
          <value>https://login.salesforce.com/serv...</value>
        </setting>
      </email2case.Properties.Settings>
    </applicationSettings>
    <system.serviceModel>
        <bindings/>
        <client/>
    </system.serviceModel>
    <startup>
      <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup><userSettings>
        <email2case.email2case.Properties.Settings>
            <setting name="enableEWSTrace" serializeAs="String">
                <value>False</value>
            </setting>
            <setting name="attachmentsPathLocal" serializeAs="String">
                <value>C:\email2case\temp\</value>
            </setting>
            <setting name="eTracePath" serializeAs="String">
                <value>C:\email2case\Trace\</value>
            </setting>
            <setting name="ewsTraceKeepDays" serializeAs="String">
                <value>1</value>
            </setting>
        </email2case.email2case.Properties.Settings>
    </userSettings>
</configuration>

I don't want to go editing that without knowing exactly the right way to do it, so what code should I use to reference my properties and settings?

Or should I alter the way they are defined in the app.config?

like image 674
Our Man in Bananas Avatar asked Dec 20 '22 01:12

Our Man in Bananas


1 Answers

The issue was caused by 2 problems:

  1. When you change the namespace, it needs to be done in the Application Settings instead of just going into the code and replacing old namespace with new namespace
  2. After incorrectly changing the namespace in the code, the namespace in the code was out of sync with the application settings, and app.config

The solution was to

  1. Change the namespace in the proper place (in the Project Properties > Application > default Namespace
  2. Clean, Rebuild, Save the project
    • If necessary, rebuild the the app.config file following the instructions in this SO post : Rebuilding app.config in visual studio?
  3. Refernce the settings and properties in code using the correct code.
    • in my case this was Properties. and NOT email2case.Properties. ...
like image 161
Our Man in Bananas Avatar answered Dec 31 '22 19:12

Our Man in Bananas