如何在FileStream实例中检查FileMode

时间:2022-07-11 07:03:11

I would like to check if FileStream got to MyMethod is in Append mode.

我想检查FileStream到MyMethod是否处于追加模式。

class MyClass
{
    public static void MyMethod(FileStream file)
    {
        if ( /* file is in `Append` mode */ )
        {
            /* doing something (ref1) */
        }
        else
        {
            /* doing something else (ref2) */
        }
    }
}

And example of using:

以及使用的例子:

MyClass.MyMethod(File.Open("x.x", FileMode.Append)); // should run code marked as `ref1`
MyClass.MyMethod(File.Open("x.x", FileMode.OpenOrCreate)); // should run code marked as `ref2`

Is it possible to check this? Is it possible to check similarly FileAccess given to File.Open method? What should I do if I have FileStream as a field of class and I want to check this in the contract invariant?

有可能检查一下吗?是否有可能同样检查File.Open方法的FileAccess?如果我将FileStream作为类的字段并且我想在合约不变量中检查这个,我该怎么办?

1 个解决方案

#1


3  

The FileStream class does not expose that information to you. You will have to remember which value of FileMode you used, and pass that information to your method.

FileStream类不会向您公开该信息。您必须记住您使用的FileMode的值,并将该信息传递给您的方法。

#1


3  

The FileStream class does not expose that information to you. You will have to remember which value of FileMode you used, and pass that information to your method.

FileStream类不会向您公开该信息。您必须记住您使用的FileMode的值,并将该信息传递给您的方法。