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

3
  • what is the type of this solution (web - console)? Commented Apr 9, 2015 at 12:24
  • @MohammedElSayed How can I find out? It doesn't open a console. My startup project is a nancyfx server running on IIS Express. Commented Apr 9, 2015 at 12:28
  • nancy can be selfhosted or iis hosted, there is nothing called nancyserver on IIS, :) if IIS MUST be running in order to get a result, then it's hosted on IIS. Commented Apr 9, 2015 at 12:32

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

7 Comments

This throws the same exception. (Updated question)
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.
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.
This covers what the TEMP ASP.NET files are stackoverflow.com/questions/450831/…
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
|
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"

1 Comment

I did do that, still getting the exception.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.