CA2202 on FileStream with StreamWriter

81 views Asked by At

I am using a filestream and streamwriter to write a small text on file it giving me CA2202 warning

    public void WritePIDToFile()
    {
        FileStream fh = null;
        try
        {
            fh = new FileStream(HIMSHelper.ApplicationDirectory + "PID", FileMode.Open, FileAccess.ReadWrite, FileShare.Read);
            using (var sr = new StreamWriter(fh))
            {
                sr.Write(PID);
            }
        }
        finally
        {
            if (fh != null)
            {
                fh.Dispose();//I got CA2202 here
            }
        }
    }

I already try the solution here CA2202: Do not dispose objects multiple times

Can you give me any suggestions? Please.

Thanks

0

There are 0 answers