FileNotFound but path is 100% okay in Blazor app

huangapple go评论53阅读模式
英文:

FileNotFound but path is 100% okay in Blazor app

问题

尝试读取项目中的文件,但出现异常。值得注意的是,这是 Blazor 项目。

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


我尝试将此文件放入我的 PC 上的其他文件夹,但仍然无法正常工作。这段代码在其他项目中正常运行。
我甚至尝试使用 Substring(1) 将开头的“/”去掉,但结果仍然是“/ppsettings.json”。
我尝试从其他项目中读取此文件,它可以正常工作,只是从这个项目中无法正常工作。

这是 .csproj 的一部分:



Always



```

这是错误信息:

System.AggregateException: 发生了一个或多个错误(找不到文件 '/appsettings.json')---&gt; System.IO.FileNotFoundException: 找不到文件 '/appsettings.json'。文件名: '/appsettings.json' 在 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:\weOwn\metaland\CarDb\Source\CarDb\CarDb.Dashboard\Program.cs:line 10 --- End of inner exception stack trace ---

<details>
<summary>英文:</summary>

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 &quot;/&quot; at the beginning, but the result was &quot;/ppsettings.json&quot;.
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, Func2 errorRewriter) at Interop.CheckIo(Error error, String path, Boolean isDirectory, Func2 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:\weOwn\metaland\CarDb\Source\CarDb\CarDb.Dashboard\Program.cs:line 10 --- End of inner exception stack trace ---



</details>


# 答案1
**得分**: 1

我假设你正在使用Blazor服务器端。

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);

我猜你知道如何使用调试器来检查路径。

<details>
<summary>英文:</summary>

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.

</details>



# 答案2
**得分**: 0

我刚刚切换到使用API而不是直接使用服务。

<details>
<summary>英文:</summary>

I just switched to APIs instead of directly using services. 

</details>



huangapple
  • 本文由 发表于 2023年5月10日 16:54:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76216562.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定