gpt4 book ai didi

c# - 在 C# 中使用 File.OpenRead() 是否需要关闭?

转载 作者:行者123 更新时间:2023-12-02 02:48:53 24 4
gpt4 key购买 nike

我没有找到有关 https://learn.microsoft.com/en-us/dotnet/api/system.io.file.openread?view=netcore-3.1 的信息

我正在检查该文件是否存在,以便可以使用它。我很快就会再次打开该文件。我想知道在检查后关闭它是否是更好的做法。

File.OpenRead()之后我需要关闭文件吗?

bool DoesFileOpensAndRead(string path)
{
try
{
using (File.OpenRead(path))
{
return true;
}
}
catch (Exception)
{
return false;
}
}

最佳答案

由于您是通过using语句使用IDisposable接口(interface),因此不需要调用CloseDispose 将为您做到这一点。

关于c# - 在 C# 中使用 File.OpenRead() 是否需要关闭?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62393868/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com