Exemplo n.º 1
0
        IsEncryptedPackageEnvelope(
            string fileName
            )
        {
            bool retval = false;

            if (fileName == null)
            {
                throw new ArgumentNullException("fileName");
            }

            StorageRoot root = null;

            try
            {
                //
                // When StorageRoot.Open is called on a file that is not a compound file,
                // it throws an IOException whose inner exception is a COMException whose
                // error code is 0x80030050, STG_E_FILEALREADYEXISTS. Check for that case
                // and return false because that means that this is not an RM-protected file.
                //
                // Any other exception is a real error. For example, StorageRoot.Open
                // throws FileNotFoundException if path does not exist, and we let that
                // flow through.
                //
                root = StorageRoot.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);

                //
                // It's a compound file. Does it contain an "EncryptedPackage" stream?
                //
                retval = ContainsEncryptedPackageStream(root);
            }
            catch (IOException ex)
            {
                COMException comException = ex.InnerException as COMException;
                if (comException != null && comException.ErrorCode == STG_E_FILEALREADYEXISTS)
                {
                    return(false);
                }

                throw;  // Any other kind of IOException is a real error.
            }
            finally
            {
                if (root != null)
                {
                    root.Close();
                }
            }

            return(retval);
        }
Exemplo n.º 2
0
        IsEncryptedPackageEnvelope(
            Stream stream
            )
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            bool        retval = false;
            StorageRoot root   = null;

            try
            {
                //
                // When StorageRoot.CreateOnStream is called on a stream that is not
                // a storage object, it throws an IOException whose inner exception is
                // a COMException whose error code is 0x80030050, STG_E_FILEALREADYEXISTS.
                // Check for that case and return false because that means that this
                // stream is not an RM-protected file.
                //
                // Any other exception is a real error.
                //
                root = StorageRoot.CreateOnStream(stream, FileMode.Open);

                //
                // It's a compound file. Does it contain an "EncryptedPackage" stream?
                //
                retval = ContainsEncryptedPackageStream(root);
            }
            catch (IOException ex)
            {
                COMException comException = ex.InnerException as COMException;
                if (comException != null && comException.ErrorCode == STG_E_FILEALREADYEXISTS)
                {
                    return(false);
                }

                throw;  // Any other kind of IOException is a real error.
            }
            finally
            {
                if (root != null)
                {
                    root.Close();
                }
            }

            return(retval);
        }