I am getting error: Could not find file ‘C:UsersUsersourcereposYoutubeSubtitlesYoutubeSubtitlesbinDebugnet7.0client_secrets.json’.
However, I have this file in that directory
And the code of how use this file:
string filePath = Directory.GetCurrentDirectory() + "\client_secrets.json";
List<string> lines = File.ReadAllLines(filePath).ToList();
I’ve tried it with different filename and file, and it doesn’t help. I also have tried to Clean, Rebuild and restarting Visual Studio.
Update
This produces the same filePath: string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "client_secrets.json"); and it didn’t solve the issue
When i copied exact path from the error message and pasted it into adressbar, it found the file.
I also reproduced this issue in different project, and with different file in different location, I am getting the same error.
2
Answers
I wrote the extension of file to the name, so it was basically client_sercets.json.json
In C# strings, the backslash () is used as an escape character. To include a literal backslash in a file path, you need to escape it by using a double backslash (). So, the correct way to construct the file path is like below.
The double backslashes in the file path are essential for the code to compile correctly. If you use a single backslash, it will result in a compilation error due to the escape character syntax. So, using double backslashes is necessary for the code to work as intended.
In summary, ensuring the proper use of double backslashes in the file path will likely resolve the issue you’re facing with the "Could not find file" error.