gpt4 book ai didi

c# - 检查 OpenFileDialog.OpenFile 是否为空

转载 作者:行者123 更新时间:2023-11-30 12:26:28 24 4
gpt4 key购买 nike

我已经在 the msdn doc 中查看了这段代码:

Stream myStream = null;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
[...] // Some init
try
{
if ((myStream = openFileDialog1.OpenFile()) != null)
{
using (myStream)
{
// Insert code to read the stream here.
}
}
}

但是 Resharper 温柔地告诉我检查 null 是没有用的:

enter image description here

我应该相信 Resharper 还是微软?

最佳答案

这是(大部分)OpenFile() 的源代码.

try block 有可能抛出异常,并且该方法返回 nullStream,所以我会相信 MSDN 文档。我不确定 ReSharper 为何提出该建议。

public Stream OpenFile()
{
string filename = FileNamesInternal[0];

if (filename == null || (filename.Length == 0))
throw new ArgumentNullException("FileName");

Stream s = null;

new FileIOPermission(FileIOPermissionAccess.Read, IntSecurity.UnsafeGetFullPath(filename)).Assert();
try
{
s = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
}
finally
{
CodeAccessPermission.RevertAssert();
}
return s;
}

关于c# - 检查 OpenFileDialog.OpenFile 是否为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28541404/

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