richard
richard

Reputation: 12526

Windows 7 User Account Control changing configuration string retrieved by ConfigurationSettings.AppSettings?

Background

I have a Windows Forms application (.NET 3.5). The database connection string is stored in the app.config file in the appSettings section with the name "connectionString".

<appSettings>

    <add key="connectionString" value="Data Source=REDACTED;Initial Catalog=REDACTED; UID=REDACTED; pwd=REDACTED;" />

</appSettings>

This is accessed in the code for database queries with the following code:

dbConnString = ConfigurationSettings.AppSettings["connectionString"];

Setup

Because I am having problems, I littered the startup and applicable sections of the application with MessageBox.Show() statements, each giving relevant information about where I am in the program and what the variable values are.

I have an installer project that creates and MSI file. I take that file, copy it to the client's machine, and install it (if the application is already installed, I uninstall it first).

The Problem

I am seeing some REALLY strange behaviour...

  1. If I run the application as administrator (right-click > Run As Administrator), the application runs as expected. It selects the expected connection string (Data Source=REDACTED;Initial Catalog=REDACTED; UID=REDACTED; pwd=REDACTED;)and connects the application to the database without issue. Notice that the connection string is using SQL Server authentication (not Windows authentication).
  2. If I just double-click the .exe file, or run it from the Start menu, I get a completely DIFFERENT connection string value. I get back this instead (IN THE SAME PLACE IN THE CODE): Data Source=REDACTED;Integrated Security=SSPI;Initial Catalog=REDACTED. Notice that this is using Windows authentication to try to connect to the database.
  3. If I delete the app.config file completely, the application still runs (huh??? how??) but with some differences:
    a. If I run as administrator, it can't find the connection string connectionString and I get the error `Object reference not set to an instance of an object.'
    b. If I run from the Start menu or by double-clicking the .exe file, it brings back the Windows authentication connection string.
  4. I decided to try something else. I changed the name of the connection string (connectionString) in the app.config file. So I named it connectionStringDude. Now when I run it in the two ways described above, again the "Run As Administrator" version works, but the other one fails to find a connection string by that name, and the application gets an `Object reference not set to an instance of an object.'.

So it appears to me that there is maybe some hierarchy that the application is using to look for the connection string connectionString. If the framework can't find the connection string in the app.config, it checks machine.config, and so on until it either finds it or doesn't.

I am tearing my hair out trying to figure this out. Why would the connection string CHANGE based just on "Run as administrator"?

And just to be clear, the SAME line of code:

dbConnString = ConfigurationSettings.AppSettings["connectionString"];

is returning a COMPLETELY different value in dbConnString depending on if I Ran as Administrator or not.

Upvotes: 0

Views: 152

Answers (1)

Ian Boyd
Ian Boyd

Reputation: 256911

Use Process Monitor to see where Windows is loading your application settings from.

Configuration Settings in .NET application are persisted in separate app.exe.config files. You likely have a stray file somewhere.

And with UAC file virtualization, that file could be sitting in something like:

C:\Users\Richard\AppData\Local\VirtualStore\Program Files(x86)\RichardWare\SuperCoolapp.exe.config

Use Process Monitor to watch all file accesses of your app, especially *.config files.

Upvotes: 1

Related Questions