Osi
Osi

Reputation: 13

Load json from the same directory as .dll

How do i load a file (json in this case) in the same directory as the .dll file that uses it?

Programfolder/Plugins

In my case, the executable is in Programfolder and the dll and json in /plugins.

I tried this:

    [System.Serializable]
    public class KeyCodes
    {
        public string RandomizeAllKey;
        public string RandomizeFaceKey;
        public string RandomizeBodyKey;
        public string RandomizeFaceBodyKey;
        public string RandomizeClothesKey;
    }

    string configFilename = "Randomizer.json";
    var configPath = $"{UserData.Path}..\\Plugins\\{configFilename}";
    var keys = JsonUtility.FromJson<KeyCodes>(System.IO.File.ReadAllText(configPath));

but it doesn't seem to work.

Upvotes: 1

Views: 1052

Answers (1)

Tam Bui
Tam Bui

Reputation: 3038

To find the path where your dll is running, use this:

// GetExecutingAssembly finds the path where THIS code is running
var dllPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string configFilename = "Randomizer.json";
var configPath = Path.Combine(dllPath, configFilename);

Upvotes: 1

Related Questions