-1

I'm trying to deserialize a JSON file which is stored within a folder in my .NET windows forms project.

Deserialize from file:

private HashSet<string> swearWords = JsonConvert.DeserializeObject<HashSet<string>>(File.ReadAllText(Directory.GetCurrentDirectory() + @"\Folder\file.json"));

I get the error:

System.IO.DirectoryNotFoundException: 'Could not find a part of the path 'C:..\Prototypes\Prototype1\Prototype1\bin\Debug\Folder\file.json'.'

When i navigate to the Debug folder, it seems that my folder containing my json files is not there. The path is correct as far as I can see, but I don't think my json files are being added when the project builds.

The folder/files are included in the project.

How do I make sure that this folder I created is being included when the project is built?

Gavin
  • 55
  • 1
  • 10
  • 2
    Please update your question to include the `Properties` of that file (from Visual Studio) - preferably as a screenshot. – mjwills Mar 20 '19 at 21:14
  • Possible duplicate of [Best way to get application folder path](https://stackoverflow.com/questions/6041332/best-way-to-get-application-folder-path) – mjwills Mar 20 '19 at 21:16
  • Is \Folder\file.json included in your solution? What is its Copy to Output Directory property? – Dour High Arch Mar 20 '19 at 21:17
  • Please show the screenshot of the `Properties` from Visual Studio. At the very least I'd expect to see a `Build Action` as well. – mjwills Mar 20 '19 at 21:18
  • 1
    Mark the json file in your source code as "Copy to output directory" as "always" . This should resolve your issue. – Manoj Choudhari Mar 20 '19 at 21:18
  • When i checked the properties, I did need to set copy to output directory. That was my issue. – Gavin Mar 20 '19 at 21:20

1 Answers1

2

Did you set the file properties to copy the content to the output directory? Check the attached image.

Copy to Output Directory

M. Ruiz
  • 406
  • 4
  • 11