有人可以告诉我 OpenRead 方法使用什么权限和文件共享来读取文件。
我正在尝试这段代码,
FileStream stream = File.OpenRead(FileName);
但被建议使用此代码,
var stream = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read);
所以我的问题是,如果我不提供其他参数,File.OpenRead() 默认使用什么。
我不能只更改生产服务器上的代码。
有人可以告诉我 OpenRead 方法使用什么权限和文件共享来读取文件。
我正在尝试这段代码,
FileStream stream = File.OpenRead(FileName);
但被建议使用此代码,
var stream = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read);
所以我的问题是,如果我不提供其他参数,File.OpenRead() 默认使用什么。
我不能只更改生产服务器上的代码。
你可以看到反编译:
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);
}
从文档
[public static FileStream OpenRead(string path)] 等效于 FileMode 值为 Open、FileAccess 值为 Read 和 FileShare 值为 Read 的 FileStream(String, FileMode, FileAccess, FileShare) 构造函数重载。