Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reading a json file exception "Unhandled Exception: System.TypeInitializationException: The type initializer for "

I'm trying to learn how to write a Discord bot and I need to read form a JSON file.

Here is my Utilities class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.IO;

namespace mah_discord_bot
{
    class Utilities
    {

        private static Dictionary<string, string> alerts;

        static Utilities()
        {
            string json = File.ReadAllText("SystemLang\alerts.json");
            var data = JsonConvert.DeserializeObject<dynamic>(json);

            alerts = data.ConvertToObject<Dictionary<string, string>>();    
        }

        public static string GetAlert(string key)
        {
            if (alerts.ContainsKey(key))
            {
                return alerts[key];
            }
            else
            {
                return "";
            }
        }
    }
}

And here is my Main class where I call the Utilities class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace mah_discord_bot
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(Utilities.GetAlert("test"));
        }
    }
}

My JSON file goes as follows

{
    "test": "Hello"
}

and my folder structure looks like this, I'm not really sure if it's because it can't read the JSON file I created, when debugging this line

string json = File.ReadAllText("SystemLang\alerts.json");

It returns null so I'm not sure where I'm messing up.

enter image description here

Also the complete error log

Unhandled Exception: System.TypeInitializationException: The type initializer for 'mah_discord_bot.Utilities' threw an exception. ---> System.ArgumentException: Illegal characters in path.
   at System.IO.Path.CheckInvalidPathChars(String path, Boolean checkAdditional)
   at System.IO.Path.GetFileName(String path)
   at System.IO.StreamReader..ctor(String path, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize, Boolean checkHost)
   at System.IO.File.InternalReadAllText(String path, Encoding encoding, Boolean checkHost)
   at System.IO.File.ReadAllText(String path)
   at mah_discord_bot.Utilities..cctor() in K:\visual sutdio proj\mah-discord-bot\mah-discord-bot\Utilities.cs:line 18
   --- End of inner exception stack trace ---
   at mah_discord_bot.Utilities.GetAlert(String key)
   at mah_discord_bot.Program.Main(String[] args) in K:\visual sutdio proj\mah-discord-bot\mah-discord-bot\Program.cs:line 13
Press any key to continue . . .
like image 974
MewTwo Avatar asked Nov 19 '25 10:11

MewTwo


1 Answers

System.ArgumentException: Illegal characters in path.

\ is used as escape character in C# and \a is not a valid path character.

"SystemLang\alerts.json"

Use @"SystemLang\alerts.json" or "SystemLang\\alerts.json" instead.

like image 61
Xiaoy312 Avatar answered Nov 20 '25 23:11

Xiaoy312



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!