Anoop Mishra
Anoop Mishra

Reputation: 1015

How to close a stream when it returns a stream

 public Stream DecryptFile(string inputFile)//, string outputFile)
    {
        {  
                string password = @"mykey"; // Your Key Here

                UnicodeEncoding UE = new UnicodeEncoding();
                byte[] key = UE.GetBytes(password);

                FileStream fsCrypt = new FileStream(inputFile, FileMode.Open);

                RijndaelManaged RMCrypto = new RijndaelManaged();

                CryptoStream cs = new CryptoStream(fsCrypt,
                    RMCrypto.CreateDecryptor(key, key),
                    CryptoStreamMode.Read);


                StreamReader sr = new StreamReader(cs);



              Stream s = sr.BaseStream;
                //sr.Close();
                //fsCrypt.Close();
             return s;
        }
    }

In this code there is a problem that stream is not closing properly. If I close it before returning the value then it throws an error.

Upvotes: 1

Views: 1780

Answers (3)

Marius
Marius

Reputation: 577

Its propably much better to realize it with usings. Usings close and dispose the underlying stream for you.

public Stream DecryptFile(string inputFile)//, string outputFile)
{
    string password = @"mykey"; // Your Key Here
    UnicodeEncoding UE = new UnicodeEncoding();
    byte[] key = UE.GetBytes(password);
    using(var fsCrypt = new FileStream(inputFile, FileMode.Open)
    {
        RijndaelManaged RMCrypto = new RijndaelManaged();
        using(CryptoStream cs = new CryptoStream(fsCrypt, RMCrypto.CreateDecryptor(key, key), CryptoStreamMode.Read))
        {
            StreamReader sr = new StreamReader(cs);
            Stream s = sr.BaseStream;
            return s;
        }
    }
}

Upvotes: 1

Alexei - check Codidact
Alexei - check Codidact

Reputation: 23098

fsCrypt.Close(); should be performed, but sr.Close(); should not be performed, since the caller of your function should be able to use the Stream.

Also, in order to properly close streams when errors occur, use a disposable context:

using (FileStream fsCrypt = new FileStream(inputFile, FileMode.Open))
{
    RijndaelManaged RMCrypto = new RijndaelManaged();

    CryptoStream cs = new CryptoStream(fsCrypt,
        RMCrypto.CreateDecryptor(key, key),
        CryptoStreamMode.Read);

    StreamReader sr = new StreamReader(cs);
    Stream s = sr.BaseStream;
    return s;
}

The caller should also use this pattern:

using (var stream = DecryptFile(string inputFile))
{
    // do something with decrypted file
}

Upvotes: 3

Mikael Puusaari
Mikael Puusaari

Reputation: 1054

using (FileStream fs = new FileStream(filePath, FileMode.Open,     FileAccess.Read, FileShare.ReadWrite))
{

//code here
}

I think it was introduced in ,NET 3.0 or so, and you don´t need to close streams anymore

Everything inside the using brackets will be automatically closed and be disposed of when the code leaves that part

Upvotes: 1

Related Questions