静态构造函数中的TypeInitializationException [英] TypeInitializationException inside static constructor

查看:134
本文介绍了静态构造函数中的TypeInitializationException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我这里有C#静态类:

Hi everybody,

I have this C# static class here:

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();
       }
   }





此代码调用OtherDirs列表:





and this code calls the OtherDirs list:

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





我在foreach上得到一个System.TypeInitializationException,通过调试应用程序,我发现了问题是 File.WriteAllText 方法。

我在清单中有这一行(所以我可以拥有管理员权限,我已经明确授予他们我的申请)



< requestedExecutionLevel level =highestAvailableuiAccess =false/>



你能用我的代码帮我吗?



非常感谢!



Jymmy097



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

推荐答案

您需要确保该目录存在。添加

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



之前


before

if (!File.Exists(OtherDirsFile))





希望这会有所帮助,

Fredrik



(作为旁注;不要在静态构造函数中执行此操作,它不适合它)。


这里的问题实际上是MainPath尚未初始化,它将是一个空值变量。



通常很难分辨哪一个导致了actaul问题,你可以以某种方式寻找Excetion加注的InnerException,它会告诉你实际的问题,至少我认为是NullReferenceException,或ArgumentNullException(因为你将null传递给参数;需要值)或Fredrik说你的文件(或目录)不存在。



我建议您在此阶段简单地设置一个断点并查找执行和执行的所有操作,它会告诉您主要问题被提出来了。
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.


这篇关于静态构造函数中的TypeInitializationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆