【问题标题】:FileStream constructor with FileOptions argument. Is this still valid .NET 5?带有 FileOptions 参数的 FileStream 构造函数。这仍然是有效的 .NET 5 吗?
【发布时间】:2021-07-12 16:27:21
【问题描述】:

根据.NET 5 documentation for the FileStream class,它仍然需要一个允许用户传入 FileOptions 参数的构造函数。

FileStream(String, FileMode, FileAccess, FileShare, Int32, FileOptions) 

但实际上,构造函数似乎并不存在。即使我导航到反编译的源,我也看不到它有谁知道这是文档疏忽还是我遗漏了什么?

请注意,如果这很重要,我正在使用 Windows 目标构建我的 .NET 5 应用程序

<TargetFramework>net5.0-windows</TargetFramework>

另外,如果重要的话,这就是我正在尝试做的事情(没有构建)

string path = Path.Combine(Folder, "temp-lock-delete-me.tmp");
_preventRenameFs = new FileStream(
    path, 
    FileAccess.ReadWrite,
    FileShare.Delete | FileShare.Write | FileShare.Read,
    4096,
    FileOptions.DeleteOnClose);

【问题讨论】:

  • FileMode 在哪里?

标签: filestream .net-5


【解决方案1】:

根据您的代码,您需要添加 FileMode 参数

string path = Path.Combine(Folder, "temp-lock-delete-me.tmp");
_preventRenameFs = new FileStream(
    path, 
    FileMode.OpenOrCreate,// <-- add FileMode
    FileAccess.ReadWrite,
    FileShare.Delete | FileShare.Write | FileShare.Read,
    4096,
    FileOptions.DeleteOnClose);

【讨论】:

  • 我一定盯着它看了 35 分钟,但从来没有看到我面前的东西。我觉得自己像个白痴。谢谢。
猜你喜欢
  • 2013-08-14
  • 2020-08-25
  • 2014-01-29
  • 2021-10-25
  • 1970-01-01
  • 1970-01-01
  • 2011-05-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多