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:
- 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.
- 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:
Encrypt the .config section. However, that only prevents casual reading of the config file.
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();
}