Resolved ConfigurationManager.AppSettings return null value, but works if run as Administrator

imonic

New member
Joined
Aug 12, 2025
Messages
3
Programming Experience
10+
Only on one server with windows server 2012 R2, I run the C# program just to read a value in app.config file under Administrator account. It returns null. But I run the program as a
Administrator, it works and returns correct value. What is the issue here? The code is:
C#:
Expand Collapse Copy
[STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            string firstFlag = "a";
            try
            {
                //MessageBox.Show("读取前 First value="+firstFlag);
                firstFlag = System.Configuration.ConfigurationManager.AppSettings["First"];
            }
            catch (Exception ex)
            {
                firstFlag = ex.ToString();
            }
            MessageBox.Show("First="+firstFlag);
            Application.Run(new Form1());
        }
and app.config is:
XML:
Expand Collapse Copy
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <appSettings>
        <add key="First" value="0" />
    </appSettings>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
    </startup>
</configuration>
 
Last edited by a moderator:
After using ChatGPT, I run the test program provided by ChatGPT, and find the issue. It uses a short 8.3 filename (DOS-style), and the config file name does not match. The problem is solved, but I just wonder how the OS suddenly turned on to short 8.3 filename.
 
It depends on how you are launching your application. Are you using a DOS window? Are you using a CMD window, but you changed directory using an 8.3 name rather than a long name? Are you using some kind of .BAT file rather than a .CMD file? Is the OS configured to run 32-bit applications with forced short names, while leaving 64-bit apps to run with long names?
 
The system is 64-bit Windows Server 2012 R2. I just double click the exe filename to run. At the beginning everything is fine. Then after a few days, this issue comes out. Now I disable the drive from generating short name, and create a new folder for program, then the program runs ok.
When this issue came up, the ASP.NET web page had the issue too: cannot find the some dll files in temp directory. I had to create a new directory for ASP.NET program, then the web page was up normally. But after a few days, the web page again had the same problem. I had to recreate a new directory. I couldn't figure out why this happen again and again.
 
Back
Top Bottom