我正在创建一个创建随机文件的简单函数.为了线程安全,它在重试循环中创建文件,如果文件存在,则再次尝试.
while (true)
{
fileName = NewTempFileName(prefix, suffix, directory);
if (File.Exists(fileName))
{
continue;
}
try
{
// Create the file, and close it immediately
using (var stream = new FileStream(fileName, FileMode.CreateNew, FileAccess.Write, FileShare.Read))
{
break;
}
}
catch (IOException e)
{
// If the error was because the file exists, try again
if ((e.HResult & 0xFFFF) == 0x00000050)
{
continue;
}
// else rethrow it
throw;
}
}
Run Code Online (Sandbox Code Playgroud)
根据MSDN,HResult值来自COM,它似乎表明它只能在Windows上运行,它特别将它们列为"Win32代码".但是这是一个面向.NET Standard的库,理想情况下它应该适用于.NET Standard支持的每个平台.
我想知道的是,我是否可以依赖上述使用HResult值的跨平台方法?关于这一点,文档并不清楚.
如果没有,我如何确定在其他平台上期望的HResult值?
注意: …