skip to Main Content

So I am trying to read the file in the project but it gives me an exception. Worth to add that this is the Blazor project.

string jsonFile = File.ReadAllText("appsettings.json");

I tried placing this file in other folders on my PC but it still is not working. This exact line of code works in other projects.
I even tried Substring(1) so it removes "/" at the beginning, but the result was "/ppsettings.json".
I tried reading this file from other projects and it is working, it is just not working from this one.

This is .csproj part:

<ItemGroup>
       <Content Update="appsettings.json">
           <CopyToOutputDirectory>Always</CopyToOutputDirectory>
       </Content>
       <None Remove="appsettings.json" />
   </ItemGroup>

This is the error:

System.AggregateException: One or more errors occurred. (Could not find file '/appsettings.json'.) ---> System.IO.FileNotFoundException: Could not find file '/appsettings.json'. File name: '/appsettings.json' at Interop.ThrowExceptionForIoErrno(ErrorInfo errorInfo, String path, Boolean isDirectory, Func`2 errorRewriter) at Interop.CheckIo(Error error, String path, Boolean isDirectory, Func`2 errorRewriter) at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String path, OpenFlags flags, Int32 mode) at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize) at System.IO.Strategies.OSFileStreamStrategy..ctor(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize) at System.IO.Strategies.UnixFileStreamStrategy..ctor(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize) at System.IO.Strategies.FileStreamHelpers.ChooseStrategyCore(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize) at System.IO.Strategies.FileStreamHelpers.ChooseStrategy(FileStream fileStream, String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, Int64 preallocationSize) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, Int64 preallocationSize) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean useAsync) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize) at System.IO.StreamReader.ValidateArgsAndOpenPath(String path, Encoding encoding, Int32 bufferSize) at System.IO.StreamReader..ctor(String path, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize) at System.IO.StreamReader..ctor(String path, Encoding encoding, Boolean detectEncodingFromByteOrderMarks) at System.IO.File.InternalReadAllText(String path, Encoding encoding) at System.IO.File.ReadAllText(String path) at Program.<Main>$(String[] args) in C:weOwnmetalandCarDbSourceCarDbCarDb.DashboardProgram.cs:line 10 --- End of inner exception stack trace ---

2

Answers


  1. Chosen as BEST ANSWER

    I just switched to APIs instead of directly using services.


  2. I assume you are using Blazor server-side.

    var path = Directory.GetCurrentDirectory();
    
    var settingsPath = Path.Combine(path, "appsettings.json")
    if (!File.Exists(settingsPath))
    {
        throw new FileNotFoundException($"Could not find {settingsPath}");
    }
    string jsonFile = File.ReadAllText(settingsPath);
    

    and I guess you know how to use a debugger to check the path.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search