Ismael Oliveira

Ismael Oliveira

  • 1.5k
  • 115
  • 7.8k

C# Config file not working

Mar 1 2023 4:28 PM

Hi, everyone.

I´ve developped a C# program and I have a problem with configuration file.
I use Visual Studio 2022 and Windows 10.
To start, the program acesses the configuration file to read some information, then, after the user enters username and password the program grants (or not) use and opens main screen.
In config file there are information about server name and DB name. Everything worked fine until some days ago. I made some changes in config file (I removed a variable) and, in my computer it works, but in another computer it does't anymore.An exception appears about connection to DB which is closed. After many prints I foud out that the program is using variables not from the config file, and I don1t know where it comes from. I have also another program which deals with this config file. This file also can't access config file correctly.
Here is a snippet of the code of this second file:


ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = Base.caminho + Base.nomeArquivo + ".exe.config";
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
string Banco = config.AppSettings.Settings["Tipo"].Value;
string Servidor = config.AppSettings.Settings["Servidor"].Value;
string Empresa = config.AppSettings.Settings["Empresa"].Value;
string Local = config.AppSettings.Settings["Local"].Value;
string Diretorio = config.AppSettings.Settings["Pasta"].Value;
MessageBox.Show("Arquivo de configuração = " + fileMap.ExeConfigFilename + "\nServidor = " + Servidor + "   Empresa = " + Empresa + "   Local = " + Local +
           "\nDiretório = " + Diretorio + "   Banco = " + Banco);
if (!VerificaBasedeDados(Banco))
{
    MessageBox.Show("Base de dados  " + Banco + "  inexistente!");
    return;
}


In the first MessageBox.Show, the variable Banco (which is the DB name) is different from what is in the config file!
The 'if' after tests if thew DB exists, but the program crashes before it.
The curious thing here is that in my computer it runs OK. In other computers it doesn't (I tried in 2). And before that change it ran OK too!
I tried to include back the variable but it didn't work.
Does anyone have any ideas?
Thanks.


Answers (3)