C# config file stopped working
-
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 the 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.
-
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 the 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.
Since you're calling
OpenMappedExeConfiguration
, I'm guessing that your code is writing to the config file at some point. (If it's not, then why not simply useConfigurationManager.AppSettings
?) I'm also guessing that your users have installed your application in the "Program Files" directory; that your application doesn't run elevated; that your application is 32-bit; and that your application doesn't contain a manifest with a "requested execution level" attribute. In which case, the users won't have permission to write to the config file, and you'll be hitting UAC virtualization: How User Account Control works (Windows) | Microsoft Learn[^] There will be a copy of the config file at a particular location within the user's profile, and that's what your application will be reading.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Since you're calling
OpenMappedExeConfiguration
, I'm guessing that your code is writing to the config file at some point. (If it's not, then why not simply useConfigurationManager.AppSettings
?) I'm also guessing that your users have installed your application in the "Program Files" directory; that your application doesn't run elevated; that your application is 32-bit; and that your application doesn't contain a manifest with a "requested execution level" attribute. In which case, the users won't have permission to write to the config file, and you'll be hitting UAC virtualization: How User Account Control works (Windows) | Microsoft Learn[^] There will be a copy of the config file at a particular location within the user's profile, and that's what your application will be reading.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Hi, Richard. Your assumptios are correct. Notice that users had access to the program and everything was fine. The configuration program (which is used to change parameters in config file) also worked without problem in my computer and in the other computers. After I made some changes (I remove one parameter) both programs stopped working in the other computers, but continue to work fine in mine. Maybe, as you say, some permissions were changed, but I have no idea of what was it. What do you suggest me to see in that article? Thank you.
-
Hi, Richard. Your assumptios are correct. Notice that users had access to the program and everything was fine. The configuration program (which is used to change parameters in config file) also worked without problem in my computer and in the other computers. After I made some changes (I remove one parameter) both programs stopped working in the other computers, but continue to work fine in mine. Maybe, as you say, some permissions were changed, but I have no idea of what was it. What do you suggest me to see in that article? Thank you.
Quote:
Windows 10 and Windows 11 include file and registry virtualization technology for apps that are not UAC-compliant and that require an administrator's access token to run correctly. When an administrative app that is not UAC-compliant attempts to write to a protected folder, such as Program Files, UAC gives the app its own virtualized view of the resource it is attempting to change. The virtualized copy is maintained in the user's profile. This strategy creates a separate copy of the virtualized file for each user that runs the non-compliant app.
Check under the
%LOCALAPPDATA%\VirtualStore
folder to see if there's a folder for your application with a copy of the config file in it. I'd also recommend updating your application to include a manifest with the "request execution level" setting so that UAC virtualization doesn't apply to your application. You'll then need to handle the exception you get if you try to write to the config file and the user doesn't have permission.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Hi, Richard. Your assumptios are correct. Notice that users had access to the program and everything was fine. The configuration program (which is used to change parameters in config file) also worked without problem in my computer and in the other computers. After I made some changes (I remove one parameter) both programs stopped working in the other computers, but continue to work fine in mine. Maybe, as you say, some permissions were changed, but I have no idea of what was it. What do you suggest me to see in that article? Thank you.
Config files don't "stop working"; something must be wrong with the code. And config files do not belong in the Program Files directory, we don't back that up. That directory should be read only for mortal men. We back up the user directory where config files supposed to go.
Bastard Programmer from Hell :suss: "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.