Click here to Skip to main content
15,903,175 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everybody,

I have this C# static class here:
C#
public static class Settings
   {
       public static List<string> OtherDirs { get; private set; }
       public static readonly string MainPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Example Folder");
       static Settings()
       {
           OtherDirs = new List<string>();
           string OtherDirsFile = Path.Combine(MainPath, "MoreDirectories.txt");
           if (!File.Exists(OtherDirsFile))
           {
               File.WriteAllText(OtherDirsFile, "");
           }
           OtherDirs = File.ReadAllLines(OtherDirsFile).ToList();
       }
   }


and this code calls the OtherDirs list:

C#
List<string> Files = Directory.EnumerateFiles(Appdata, "*.txt", SearchOption.AllDirectories).ToList();
            foreach (string dir in Settings.OtherDirs)
            {
                Files.AddRange(Directory.EnumerateFiles(dir, "*.txt", SearchOption.AllDirectories));
            }


I get a System.TypeInitializationException at the foreach and, by debugging the application, I found out tha tthe problem is the File.WriteAllText method.
I have this line in the manifest (so I can have admin rights and I've obiviously granted them to my application)

<requestedExecutionLevel level="highestAvailable" uiAccess="false" />

Can you help me with my code?

Thanks a lot!

Jymmy097
Posted
Comments
TheRealSteveJudge 17-Dec-14 8:34am    
The constructor of a static class is executed the first time the static class
is used.
Can you provide more exception details from the File.WriteAllText method?
Praveen Kumar Upadhyay 17-Dec-14 8:50am    
The Error might be in the for loop at Settings.OtherDirs. Is it compulsory to declare Settings class as static? If not then remove static from the class and from the constructor, it should work.
Tomas Takac 17-Dec-14 9:45am    
What's the inner exception? Maybe you should post the whole exception dump.
LLLLGGGG 18-Dec-14 11:39am    
Actually there's no inner exception. i tried to debug the ctor line by line, but the exception remains the same.

You need to make sure the directory exists. Add
Directory.CreateDirectory(MainPath);

before
if (!File.Exists(OtherDirsFile))


Hope this helps,
Fredrik

(as a side note; do not do this in a static constructor, it's not the place for it).
 
Share this answer
 
Comments
LLLLGGGG 18-Dec-14 11:39am    
Thanks.

I have already tried to put the lines to create the directory without success... Now I have not got the possibility to test your solutions. As soon as I can I'll try to solve the problem.

Ps. I'm going to remove the static ctor :-)
The problem here actually is that the MainPath is not yet initialized and it will be a null valued variable.

Usually it is really hard to tell which one is causing the actaul problem, you can somehow look for the InnerException of the Excetion raise, which will tell you about the actual problem, which atleast I believe is the NullReferenceException, or the ArgumentNullException (because you're passing a null to the parameter; where a value is needed) or as Fredrik has said your file (or directory) doesn't exist.

I would recommend that you simply put a break point at this stage and look for all of the actions that are executed and performed, it will tell you about the main problem which was raised.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900