如何解决File.Exists返回true,但是File.Encrypt抛出
我第一次在Windows Server 2016上部署了新的ASP.NET Core 3.1应用程序。但是,该应用程序在启动时崩溃,并在appsettings.json加密期间抛出500.30错误和异常。如下面的代码片段所示,File.Exists
返回true,但是,在File.Encrypt
中引发了异常。我确保在IIS的应用程序池中定义的用户帐户对文件夹具有完全控制权,但是似乎没有任何作用。
private static void EncryptAppSettings()
{
var path = Path.Combine(AppContext.BaseDirectory,"appsettings.json");
if (File.Exists(path))
{
File.Encrypt(path); // Unhandled exception. System.IO.IOException: Element not found. : 'P:\Balsam\appsettings.json'
}
}
文件本身当然位于指定的路径下。 来自事件查看器的完整信息:
Application: Balsam.Front.exe
CoreCLR Version: 4.700.20.41105
.NET Core Version: 3.1.8
Description: The process was terminated due to an unhandled exception.
Exception Info: System.IO.IOException: Element not found. : 'P:\Balsam\appsettings.json'
at System.IO.FileSystem.ThrowExceptionEncryptDecryptFail(String fullPath)
at System.IO.FileSystem.Encrypt(String path)
at System.IO.File.Encrypt(String path)
at Balsam.Front.Program.EncryptAppSettings() in P:\JENKINS\x\Balsam.Front\Program.cs:line 40
at Balsam.Front.Program.Main(String[] args) in P:\JENKINS\x\Balsam.Front\Program.cs:line 16
如果File.Encrypt
返回true,为什么File.Exists
会引发异常?
解决方法
不要认为问题出在基于文档的您编写的代码上。
https://docs.microsoft.com/en-us/dotnet/api/system.io.file.exists?view=netcore-3.1 https://docs.microsoft.com/en-us/dotnet/api/system.io.file.encrypt?view=netcore-3.1
特别阅读File.Encrypt引发的异常,当它是IO异常时,问题就不在于找不到文件(为此有很多其他异常)。
IOException打开文件时发生I / O错误。
-或-
当前平台不支持此操作。
所以,不太确定是什么问题。 File.Encrypt引发的所有其他常见问题也有例外,因此我认为问题完全是另外一回事。
也许这是.NET Core中的错误,类似于注释中提到的错误? NET可能需要其中一种服务吗?无论哪种方式,就我所知,这个错误都不是我们可以明确解决的。
我会在git上引发一个错误。也许将其链接到此答案中,这样,如果您选择的是这种方式,那么将来遇到此问题的人就会更加了解。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。