1

Right now I have a small Console application that connects to my database. When debugging the application in Visual Studio I can have this in my app.config:

<connectionStrings>
    <add name="Default"
         connectionString="Data Source=*******;Initial Catalog=*******;User Id=*******Password=*******;"/>
</connectionStrings >

And I can get the connection string like this:

string testConnectiongString = ConfigurationManager.ConnectionStrings["Default"].ConnectionString;

This works great when running the application In visual studio, how ever when I change to release and move the .exe file onto my laptop or just out of the Visual Studio I get:

object reference not set to an instance of an object

And that is due the connection string, I have to hard code the string in the code for it to work.

So, is there anyway to use app.config when releasing my build?

5
  • 3
    Did you deploy the app.config besides your executable with the name yourapp.exe.config where yourapp is the actual name of your executable. Both files must be in the same directory. Commented Feb 14, 2016 at 8:40
  • Where did you put that App.config? Project of application you're running or maybe some dll? You need first Commented Feb 14, 2016 at 8:41
  • App.config becomes yourAppName.exe.config. (look inside the RELEASE/DEBUG folders to find it) This is the file to distribute with your application. Did you do this? Commented Feb 14, 2016 at 8:43
  • Makes a lot of sense now, I was not aware of this. But I've read you are supposed to store the connection string inside the app.config so it will hard to get the connection string. But as I need to send the App.Config with the application they can just open the the file with any text editor and get the connection string even easier? Commented Feb 14, 2016 at 8:46
  • I don't get it. You store the connection info in the App.Config while you build your application to not HARD-CODE your connection info. You deploy YourAppName.exe.config to adapt your connection info to the customer environment so your program could adapt without requiring changes to its code. Commented Feb 14, 2016 at 9:03

1 Answer 1

2

TLDR version:

So, is there anyway to use app.config when releasing my build?

Yes, give users both the MyApp.exe and MyApp.exe.config files found in bin\Release after building.


You have two problems:

  1. When you're running from Visual Studio, you're running the .exe alongside a .config, but apparently when you're distributing your app you only distribute the .exe.
  2. If you'd distribute the .config, folks could just read it and get the connection string details.

The first problem is trivial to solve once you see it.

For context, what you should realize, if you you create a fresh console application and build it both in debug and release, the resulting folder structure will be this:

\MyFancyApp\App.config
\MyFancyApp\MyFancyApp.csproj
\MyFancyApp\MyFancyApp.sln
\MyFancyApp\Program.cs

\MyFancyApp\bin
\MyFancyApp\bin\Debug
\MyFancyApp\bin\Debug\MyFancyApp.exe
\MyFancyApp\bin\Debug\MyFancyApp.exe.config       << !

\MyFancyApp\bin\Release
\MyFancyApp\bin\Release\MyFancyApp.exe.config     << !

Note that your app.config file has been renamed to match the executable. You need it to get the connection string. Your application thus includes both these files. It would also include additional DLLs if you have NuGet packages or secondary class library projects that compile to DLLs.

The second problem is less trivial to solve. Here are two potential solutions:

  1. Encrypt the .config section. However, that only prevents casual reading of the config file.

  2. Do not store the credentials in the config, but ask the user to input them. You could use #IF DEBUG instructions to skip that bit when developing.

There are several other options of course, but I think discussing those may be out of scope for this question or SO in general.


PS. If you're looking to prevent the actual NullReferenceException you're getting, you could check for null, e.g.:

static void Main(string[] args)
{
    if (ConfigurationManager.ConnectionStrings["Default"] == null)
    {
        Console.WriteLine("Missing connection string configuration");
        Console.ReadKey();
        return;
    }

    string testConnectiongString = ConfigurationManager.ConnectionStrings["Default"].ConnectionString;
    Console.WriteLine("Doing work...");
    Console.ReadKey();
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.