I´ve developped a C# program and I have a problem with configuration file. I use Visual Studio 2022 and Windows 10. The DB is SQL Server. 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, with no erros, but in another computer it does't work anymore.An exception appears about connection to DB which is closed. The error message is 'ExecuteReader requires an open and available connection.The state of this connection is closed.' After many prints I foud out that the program is using variables not from the config file, and I don't know where it comes from. I have also another program which deals with this config file to change som variables. 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.