0

I'd like to add a config.json file to my project and have a class read from the file.

I added the file in VS and added the Build Action = Content and Copy to output directory = Copy always properties.

When building, the file is copied to C:\...\mysolution\myproject\bin\Debug.

But File.ReadAllText(@"config.json") throws a FileNotFoundException. Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); doesn't return the folder where the file is copied: C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\Temporary ASP.NET Files\\root\\2158bea8\\d46fc01c\\assembly\\dl3\\eca14761\\49a747bf_be72d001

Toast
  • 596
  • 2
  • 19
  • 39

2 Answers2

2

"\config.json" will read from ROOT, try "config.json"

rory.ap
  • 34,009
  • 10
  • 83
  • 174
Steve Drake
  • 1,968
  • 2
  • 19
  • 41
  • This throws the same exception. (Updated question) – Toast Apr 09 '15 at 12:22
  • so, the CLASS has the output file? you need to make sure its in the WORKING folder of the Application. Or if its ASP.NET which it looks like it is then the web root. – Steve Drake Apr 09 '15 at 12:24
  • it will not end up in C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\Temporary ASP.NET Files\\root\\2158bea8\\d46fc01c\\assembly\\dl3\\eca14761\\49a747bf_be72d001 (run time build) As its the BUILD of the WEB SITE that is putting the file in that folder. – Steve Drake Apr 09 '15 at 12:24
  • This covers what the TEMP ASP.NET files are http://stackoverflow.com/questions/450831/what-is-the-temporary-asp-net-files-folder-for – Steve Drake Apr 09 '15 at 12:26
  • The linked answer says that this temp folder doesn't need any maintainance. How can I get VS to copy the files into that folder? @Steve Drake – Toast Apr 09 '15 at 14:11
  • Make the config.json part of the ASP.NET project. I am sure someone suggested that earlier. – Steve Drake Apr 09 '15 at 14:31
  • I'm not sure if my solution has an ASP.NET project. (Is NancyFX ASP.NET?). However, my project is not the server project and I want it to be used by different servers. The config file shoulld only be for this project (not the entire solution). I'm looking for a way to have a config file for just the project. – Toast Apr 09 '15 at 17:56
0

as Steve said, you need to use "config.json", this will read from where the exe or application resides.

So you need to tell VS to copy this file to your output directory when building, to this do the following:

Right click the file in VS > Properties > Set "Copy to output Directory" to "Copy Always"

Tamim Al Manaseer
  • 3,554
  • 3
  • 24
  • 33