1

I have a local project that in the future will be in a pipeline. Because of this, I need to use a relative path to get and read a json file. But using the File.ReadAllText I'm obtaining the following answer:

> File.ReadAllText("MyJsonFiletoRead.json", Encoding.Default) 
System.IO.FileNotFoundException: Could not find file 'C:\Users\15071\MyJsonFiletoRead.json'

Note that 'C:\Users\15071' is not the project folder, this is my windows user folder. My struct is here:

C:\Projetcs\MyProjectTest  -->> Project folder

C:\Projetcs\MyProjectTest\MyClass.cs  -->> The class where I'm calling the ReadAllText

C:\Projetcs\MyProjectTest\MyJsonFiletoRead.json -->> My json file that I'm trying to find

I have tried the following commands to check my PATH, but all answer is wrong:

> Environment.CurrentDirectory
 "C:\\Users\\15071"

> Directory.GetCurrentDirectory()
 "C:\\Users\\15071"

AppDomain.CurrentDomain.BaseDirectory
"c:\\program files (x86)\\microsoft visual studio\\2019\\community\\common7\\ide\\commonextensions\\microsoft\\managedlanguages\\vbcsharp\\languageservices\\DesktopHost\\"

Has somebody a solution to fix this?

Note: If I use the full path, it works:

File.ReadAllText("C:/Projetcs/MyProjectTest/MyJsonFiletoRead.json", Encoding.Default)
k_pedron
  • 57
  • 1
  • 10
  • How are you running your program? And where is it located? – CodeCaster Jun 29 '20 at 20:36
  • Does this answer your question? [How to get files in a relative path in C#](https://stackoverflow.com/questions/3259583/how-to-get-files-in-a-relative-path-in-c-sharp) – austin wernli Jun 29 '20 at 20:37
  • My project is located in: C:\Projetcs\MyProjectTest\ and I'm running using the NUNIT (it's an automation test project). But to test without running tests, I tried the commands above using C# interactive. – k_pedron Jun 29 '20 at 20:39
  • 1
    @k_pedron but do you also start the executable from that directory? – CodeCaster Jun 29 '20 at 20:40
  • 1
    where are located the running program? (not you pojrect, just the "exe file" ) – Paulo Jun 29 '20 at 20:41
  • @Paulo I don't have a exe file, because this is an automation project, where I will run test routines. – k_pedron Jun 29 '20 at 20:43
  • Are you using Resharper? – Paulo Jun 29 '20 at 20:48
  • @Paulo No, I don't know about. – k_pedron Jun 29 '20 at 20:51
  • 1
    tried to do this: [Test] public void ExportationTest() { Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory); var evan= System.Environment.CurrentDirectory; //... } – Paulo Jun 29 '20 at 20:52
  • @Paulo This returns: evan "C:\\Projetcs\\MyProject\\bin\\Debug" – k_pedron Jun 29 '20 at 21:13
  • @Paulo I don't know why it is getting \bin\Debug. Do you know? – k_pedron Jun 29 '20 at 21:18
  • is getting \bin\debug because your program are running on this folder. is correct now! by default all aplications in debug mode on visual studio execute in \bin\debug. – Paulo Jun 29 '20 at 21:55
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/216905/discussion-between-k-pedron-and-paulo). – k_pedron Jun 29 '20 at 21:58

2 Answers2

1

If the file is located at the same folder of your executable file, you just need to pass the file name:

File.ReadAllText("MyJsonFiletoRead.json", Encoding.Default); 

If the file is located at a relative path from the folder where your executable file is located, you can get the Assembly Location and combine it with a relative path:

var path = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "..\\..\\MyJsonFiletoRead.json");
File.ReadAllText(path, Encoding.Default);
Murilo Maciel Curti
  • 2,677
  • 1
  • 21
  • 26
  • thanks! Exactly the file is located at a relative path... then doing your second suggestion worked to me! – k_pedron Jun 30 '20 at 01:09
-1

You need to find the current folder and then read the file

var currentFolder = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)
File.ReadAllText(currentFolder + "/MyJsonFiletoRead.json")
  • This not work. "System.NullReferenceException: 'Object reference not set to an instance of an object.' ", I guess that is a problem with Assembly – k_pedron Jun 29 '20 at 21:45