I made a little game and build it on release. In the Release folder, the executable works fine. But, if I copy it on the desktop or on another computer, I have an error pop-up saying that the program has stopped working with the details :
Signature du problème :
Nom d’événement de problème: CLR20r3
Signature du problème 01: blockMenu.exe
Signature du problème 02: 22.214.171.124
Signature du problème 03: 5a21458a
Signature du problème 04: mscorlib
Signature du problème 05: 4.7.2114.0
Signature du problème 06: 59a638b8
Signature du problème 07: 165d
Signature du problème 08: fc
Signature du problème 09: System.IO.DirectoryNotFound
Version du système: 6.1.7601.2.1.0.256.48
Identificateur de paramètres régionaux: 1036
Information supplémentaire n° 1: 0a9e
Information supplémentaire n° 2: 0a9e372d3b4ad19135b953a78882e789
Information supplémentaire n° 3: 0a9e
Information supplémentaire n° 4: 0a9e372d3b4ad19135b953a78882e789
I search on the net and saw a lot of things like :
But nothing works for my case.
At the beginning, I tested a few thing and the build worked. The only thing I changed since that moment are :
add 2 packages Newtonsoft.Json.10.0.3 and MathNet.Numerics.3.20.0
move some of my .cs files into folder, but I paid attention to namespace because I can launch it from the IDE.
I’m at the end of my research and my ideas. I’m kind of disappointed. If anything have an idea, I take it right away and I would be pleased.
Are you copying all the other files in that folder to the desktop as well? Or just the exe itself? If you’re copying just the EXE, that’s why. It can’t find MonoGame or any of your other dependencies, and if you’re doing things with content pipeline, it can’t find your Content folder either. So it’ll crash with that error.
Ok, I found the problem. I use a JSON file to get my data for the titles, menu, etc. But I wasn’t able to use it through the ContentPipeline. So I just used it in the root file.
When I built on release, the JSON file is concatenated in the exe file so my program can’t find it.
After a lot of research I found that MonoGame still can’t use JSON file in the ContentPipeline. You have to make a custom importer.
But it’s too dawn complex for me. So, instead I’ll be using an xml file which can be manage through the ContentPipeline.
You can very easily get around the fact MonoGame doesn’t have a content importer for JSON/text files. Simply add the JSON file to your Visual Studio solution explorer inside the Content folder (where your Content.mgcb file is), and set its Build Action to Content and Copy to Output Directory to Copy if newer.
When you build your game, that file will be copied to the build output in the Content folder of your game (where all the .xnb files are).
Then, to read the file:
var baseDirectory = AppDomain.CurrentDomain.BaseDirectory; //get the base directory of your game's executable.
var fullPath = Path.Combine(baseDirectory, "Content", "Name of json file.json"); //If your game's EXE is in "C:\Game", this will return "C:\Game\Content\Name of json file.json". If you're on Linux/Mac and it's in "/home/user/game", this will return "/home/user/game/Content/Name of json file.json".
string jsonData = File.ReadAllText(fullPath);
//Now you have the JSON. Do what you'd like with it :)
Bit less elegant than Content Pipeline but it should work even across platforms as long as the file actually exists.