【问题标题】:FileStream stream = File.OpenRead(FileName);FileStream 流 = File.OpenRead(FileName);
【发布时间】:2013-09-27 11:25:10
【问题描述】:

谁能告诉我 OpenRead 方法读取文件的权限和文件共享是什么。

我正在尝试这段代码,

FileStream stream = File.OpenRead(FileName);

但被建议使用此代码,

var stream = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read);

所以我的问题是,如果我不提供其他参数,File.OpenRead() 默认使用什么。

我不能只更改生产服务器上的代码。

【问题讨论】:

    标签: c# web-services .net-2.0


    【解决方案1】:

    来自documentation

    [public static FileStream OpenRead(string path)] 等价于 FileStream(String, FileMode, FileAccess, FileShare) 构造函数重载,FileMode 值为 打开,FileAccess 值为 Read,FileShare 值为 Read。

    【讨论】:

      【解决方案2】:

      可以看到反编译:

      public static FileStream OpenRead(string path)
      {
            return new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
      }
      

      和第二个一样:

      public static FileStream Open(string path, FileMode mode, FileAccess access, FileShare share)
      {
            return new FileStream(path, mode, access, share);
      }
      

      【讨论】:

      • 有据可查的情况下为什么要反编译here
      • 此外,您确信 100% 的结果。文档往往被弃用(公平地说,Microsoft Library 不太可能)。我只相信代码!
      猜你喜欢
      • 2012-02-03
      • 2013-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多