C#C
C#12mo ago
107 replies
Erich21

✅ Newtonsoft JSON - file not found

i have this code inside a Song.cs class
public void load_from_file(string path)
{
    string newPath = System.IO.Path.Combine(Environment.CurrentDirectory, path);
    try
    {
        MessageBox.Show(newPath);
        // Read the JSON file
        string jsonContent = File.ReadAllText(@newPath);

        // Deserialize the JSON content into a Person object using Newtonsoft.Json
        Song currentSong = JsonConvert.DeserializeObject<Song>(jsonContent);

        if (currentSong != null)
        {
            // Display the properties in the TextBox

            id = currentSong.id;
            title = currentSong.title;
            artist = currentSong.artist;
            bpm = currentSong.bpm;
            duration = currentSong.duration;
            audioPath = currentSong.audioPath;
            beatCount = currentSong.beatCount;
            string newAudioPath = System.IO.Path.Combine(Environment.CurrentDirectory, audioPath);

            waveSource = CodecFactory.Instance.GetCodec(newAudioPath);
            soundOut = new WasapiOut() { Latency = 100 };
            soundOut.Initialize(waveSource);

        }
        else
        {
            MessageBox.Show("Error: Could not deserialize the JSON.");
        }
    }
    catch (JsonException ex)
    {
        MessageBox.Show($"Invalid JSON format. Error: {ex.Message}");
    }
    catch (Exception ex)
    {
        MessageBox.Show($"Error: {ex.Message}");
    }
}

and my main
public MainWindow()
 {
     InitializeComponent();
     Song test = new Song();
     test.load_from_file("\\songs\\OMG.json");
     Main();

 }

I get this error when i should get a patch relative to my project, right?
image.png
image.png
Was this page helpful?