C
C#3w ago
Erich21

✅ Newtonsoft JSON - file not found

i have this code inside a Song.cs class
c#
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}");
}
}
c#
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
c#
public MainWindow()
{
InitializeComponent();
Song test = new Song();
test.load_from_file("\\songs\\OMG.json");
Main();

}
c#
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?
No description
No description
65 Replies
Angius
Angius3w ago
A path relative to the project would be more along the lines of ./songs/OMG.json or sonds/OMG.json / or \ implies root, in case of Windows it's the root of the current drive Ah, wait, you're using CurrentDirectory... Try Path.Join instead of Path.Combine .Combine discards previous root if a new root is found somewhere in the path parts, so it's possible it discarded .CurrentDirectory when it encountered a part that starts with \\ Path.Join just joins it straight
Erich21
Erich21OP3w ago
No description
Angius
Angius3w ago
uh Are you using some ancient version of .NET? You must be, Path.Join exists since Core 3.0
Erich21
Erich21OP3w ago
.net framework 4.8.1
Angius
Angius3w ago
So yes, you are using an ancient version of .NET That's probably why you also needed Newtonsoft instead of using the built-in JSON serializer $newproject
MODiX
MODiX3w ago
When creating a new project, prefer using .NET over .NET Framework, unless you have a very specific reason to be using .NET Framework. .NET Framework is now legacy code and only get security fix updates, it no longer gets new features and is not recommended. https://cdn.discordapp.com/attachments/569261465463160900/899381236617855016/unknown.png
Angius
Angius3w ago
For reference
Erich21
Erich21OP3w ago
is it possible to upgrade the code instead of making a brand new one and copy pasting?
Angius
Angius3w ago
You can try to continue using .Combine, but with a path without any slashes in front, so
test.load_from_file("songs\\OMG.json")
test.load_from_file("songs\\OMG.json")
It's not always easy, but it is possible
Angius
Angius3w ago
.NET Upgrade Assistant Overview - .NET Core
Learn more about .NET Upgrade Assistant for .NET-related projects. This tool helps you upgrade from older versions of .NET, including .NET Framework, to the latest version of .NET. Code incompatibilities can be fixed as part of the upgrade.
Erich21
Erich21OP3w ago
also i was told to use .net framework because im also using teh kinect sdk
MODiX
MODiX3w ago
Erich21
Hello, I'm currently trying to create a project using kinect for windows (I know that it's dicsontinued, but I find it interesting). However, just trying to follow a basic tutorial I ran into this, and I don't know how to fix it. When searching it up online, I don't understand what I'm supposed to do to resolve it. I'm using Visual Studio 2022, and I made a .NET 6.0 WPF project
From Erich21
React with ❌ to remove this embed.
Angius
Angius3w ago
Ah, well, RIP
Erich21
Erich21OP3w ago
well now its just
No description
Erich21
Erich21OP3w ago
altho im just very confused
Angius
Angius3w ago
See what newPath ends up being
Erich21
Erich21OP3w ago
because the message box right above the readalltext is correct
Angius
Angius3w ago
Place a breakpoint and check it in the debugger
Erich21
Erich21OP3w ago
No description
Angius
Angius3w ago
Is it correct? If so, then you're simply not copying OMG.json In the solution explorer, right-click it, open properties, and mark it to be copied always
Erich21
Erich21OP3w ago
C:\Users\Erich21\source\repos\WpfApp3\WpfApp3\bin\Debug\songs\OMG.json
Mąż Zuzanny Harmider Szczęście
is the json file there?
Erich21
Erich21OP3w ago
but if i want to add more files in the future, shouldnt the code already work like this? In cases where I end up adding lots more, do i have to manually add them to the project too
Mąż Zuzanny Harmider Szczęście
go into the exact path and check
Erich21
Erich21OP3w ago
No description
Angius
Angius3w ago
IIRC you can set the whole directory to be copied
Erich21
Erich21OP3w ago
if i go to "add existing item" it makes me select only one file
Mąż Zuzanny Harmider Szczęście
u can do it twice
Marvin
Marvin3w ago
<ItemGroup>
<Content Include="songs\*.*">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<Content Include="songs\*.*">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
put this into your .csproj and it copies on each build everything inside to your Build dir
Erich21
Erich21OP3w ago
this worked
No description
Erich21
Erich21OP3w ago
ty however still no luck :,)
Mąż Zuzanny Harmider Szczęście
whats the issue now?
Erich21
Erich21OP3w ago
.
Mąż Zuzanny Harmider Szczęście
wdym by "it worked" then?
Erich21
Erich21OP3w ago
as in, my folders are properly organised and copied in the output directory
Mąż Zuzanny Harmider Szczęście
oh, ok try adding the files to the project
Erich21
Erich21OP3w ago
they are both in the project directory and the output directory
Erich21
Erich21OP3w ago
No description
No description
Mąż Zuzanny Harmider Szczęście
i ment inside solution explorer Project>Add existing elements
Erich21
Erich21OP3w ago
No description
Mąż Zuzanny Harmider Szczęście
hmm then you can just do songs/OMG.json try it
Erich21
Erich21OP3w ago
same "file not found" message
Mąż Zuzanny Harmider Szczęście
wierd right click file and show propertie s
Erich21
Erich21OP3w ago
No description
Mąż Zuzanny Harmider Szczęście
try changing Build Action to Resource
Erich21
Erich21OP3w ago
No description
Erich21
Erich21OP3w ago
bump
qqdev
qqdev3w ago
@Erich21 Is that path correct? What happens when you paste that into Windows-Key + R, followed by hitting the enter key? And can you show the current code again?
Erich21
Erich21OP3w ago
No description
Erich21
Erich21OP3w ago
c#
public MainWindow()
{
InitializeComponent();
Song test = new Song();
test.load_from_file("songs\\OMG.json");
Main();
}
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}");
}
}
c#
public MainWindow()
{
InitializeComponent();
Song test = new Song();
test.load_from_file("songs\\OMG.json");
Main();
}
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}");
}
}
hold on my file got deleted??? ok i put it back
Erich21
Erich21OP3w ago
it opens notepad to let me edit the file
No description
Erich21
Erich21OP3w ago
C:\Users\Erich21\source\repos\WpfApp3\WpfApp3\bin\Debug\songs\OMG.json
C:\Users\Erich21\source\repos\WpfApp3\WpfApp3\bin\Debug\songs\OMG.json
qqdev
qqdev3w ago
I have no clue why it is throwing Sth must be wrong obviously Can you debug and show us the value of newPath?
Erich21
Erich21OP3w ago
No description
qqdev
qqdev3w ago
And you are still sure that the file exists? Because you mentioned that it was deleted which sounds interesting to me
Erich21
Erich21OP3w ago
yes doube checked rn i have no problem going on voice chat or something ok update: and this is something very weird
Erich21
Erich21OP3w ago
c#
public void load_from_file(string path)
{
//string newPath = System.IO.Path.Combine(Environment.CurrentDirectory, path);
string newPath = System.IO.Path.Combine(Environment.CurrentDirectory, path);
if (!File.Exists(newPath))
{
MessageBox.Show($"File does not exist at: {newPath}");
return;
}
else
{
MessageBox.Show($"File content: {File.ReadAllText(newPath)}");
}

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>(File.ReadAllText(newPath));

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;
beats = currentSong.beats;
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.\nPath:{newPath}");
}
}
catch (JsonException ex)
{
MessageBox.Show($"Invalid JSON format. Error: {ex.Message}\nPath:{newPath}");
}
catch (Exception ex)
{
MessageBox.Show($"Error: {ex.Message}\nPath:{newPath}");
}
}
public MainWindow()
{
InitializeComponent();
Song test = new Song();
test.load_from_file(@"songs\OMG.json");
Main();

}
c#
public void load_from_file(string path)
{
//string newPath = System.IO.Path.Combine(Environment.CurrentDirectory, path);
string newPath = System.IO.Path.Combine(Environment.CurrentDirectory, path);
if (!File.Exists(newPath))
{
MessageBox.Show($"File does not exist at: {newPath}");
return;
}
else
{
MessageBox.Show($"File content: {File.ReadAllText(newPath)}");
}

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>(File.ReadAllText(newPath));

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;
beats = currentSong.beats;
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.\nPath:{newPath}");
}
}
catch (JsonException ex)
{
MessageBox.Show($"Invalid JSON format. Error: {ex.Message}\nPath:{newPath}");
}
catch (Exception ex)
{
MessageBox.Show($"Error: {ex.Message}\nPath:{newPath}");
}
}
public MainWindow()
{
InitializeComponent();
Song test = new Song();
test.load_from_file(@"songs\OMG.json");
Main();

}
No description
No description
Erich21
Erich21OP3w ago
how come is it giving a file not found error/exception if it just managed to read its contents? and the usage of these functions comes from the actual documentation https://www.newtonsoft.com/json/help/html/DeserializeWithJsonSerializerFromFile.htm
qqdev
qqdev3w ago
Where does it actually throw the exception? Did you debug that? Because we have to make sure that the code after the deserialization is not causing it The handling of the exception is a bit misleading because it might not be newPath which is causing it here I usually tend to log ex.ToString() because that also includes the stack trace which is often times really helpful. Especially when dealing with rare exceptions
Erich21
Erich21OP3w ago
No description
Erich21
Erich21OP3w ago
you were right it was trying to find the audio path, not json path it was such a small detail i didnt even notice after so many tries this helped so much tysm im so sorry for not noticing it before
qqdev
qqdev3w ago
You are welcome! Happens to the best :when: !close
Accord
Accord3w ago
Closed!

Did you find this page helpful?