Code:
                         / Dotnetfx_Win7_3.5.1 / Dotnetfx_Win7_3.5.1 / 3.5.1 / DEVDIV / depot / DevDiv / releases / Orcas / NetFXw7 / wpf / src / Framework / MS / Internal / AppModel / ContentFilePart.cs / 1 / ContentFilePart.cs
                        
                        
                            //------------------------------------------------------------------------------ 
//
// 
//    Copyright (C) Microsoft Corporation.  All rights reserved.
//   
//
// Description: 
// ContentFilePart is an implementation of the abstract PackagePart class. It contains an override for GetStreamCore. 
//
// History: 
//  6/16/2005: Erichar - Initial creation.
//
//-----------------------------------------------------------------------------
 
using System;
using System.IO.Packaging; 
using System.Windows; 
using System.Windows.Resources;
using System.IO; 
using System.Resources;
using System.Globalization;
using System.Security;
using System.Security.Permissions; 
using System.Windows.Navigation;
using System.Diagnostics; 
using System.Reflection; 
namespace MS.Internal.AppModel 
{
    /// 
    /// ContentFilePart is an implementation of the abstract PackagePart class. It contains an override for GetStreamCore. 
    ///  
    internal class ContentFilePart : System.IO.Packaging.PackagePart 
    { 
        //-----------------------------------------------------
        // 
        //  Public Constructors
        //
        //-----------------------------------------------------
 
        #region Public Constructors
 
        ///  
        /// Critical    -   Accesses member _fullPath.
        /// TreatAsSafe -   Initializing _fullPath to null is safe 
        ///  
        [SecurityCritical, SecurityTreatAsSafe]
        internal ContentFilePart(Package container, Uri uri) :
                base(container, uri) 
        {
            Invariant.Assert(Application.ResourceAssembly != null, "If the entry assembly is null no ContentFileParts should be created"); 
            _fullPath = null; 
        }
 
        #endregion
        //------------------------------------------------------
        // 
        //  Protected Methods
        // 
        //----------------------------------------------------- 
        #region Protected Methods 
        /// 
        /// Critical    -   Calls critical methods GetEntryAssemblyLocation() and CriticalOpenFile()
        ///                 and accesses critical member _fullPath. 
        /// TreatAsSafe -   The Uri supplied at construction is read only and must be on the list
        ///                 of loose content files supplied at application compile time.  It is ok 
        ///                 to return the stream because we know that the stream will be read only 
        ///                 and cannot be used to get the application into an invalid state.
        ///   
        [SecurityCritical, SecurityTreatAsSafe]
        protected override Stream GetStreamCore(FileMode mode, FileAccess access)
        {
            Stream stream = null; 
            if (_fullPath == null) 
            { 
                // File name will be a path relative to the applications directory.
                // - We do not want to use SiteOfOriginContainer.SiteOfOrigin because 
                //   for deployed files the  files are deployed with the application.
                Uri codeBase = GetEntryAssemblyLocation();
                string assemblyName, assemblyVersion, assemblyKey; 
                string filePath;
 
                // For now, only Application assembly supports content files, 
                // so we can simply ignore the assemblyname etc.
                // In the future, we may extend this support for regular library assembly, 
                // assemblyName will be used to predict the right file path.
                BaseUriHelper.GetAssemblyNameAndPart(Uri, out filePath, out assemblyName, out assemblyVersion, out assemblyKey);
 
                // filePath should not have leading slash.  GetAssemblyNameAndPart( ) can guarantee it.
                Uri file = new Uri(codeBase, filePath); 
                _fullPath = file.LocalPath; 
            }
 
            stream = CriticalOpenFile(_fullPath);
            if (stream == null)
            { 
                throw new IOException(SR.Get(SRID.UnableToLocateResource, Uri.ToString()));
            } 
 
            return stream;
        } 
        protected override string GetContentTypeCore()
        {
            return MS.Internal.MimeTypeMapper.GetMimeTypeFromUri(new Uri(Uri.ToString(),UriKind.RelativeOrAbsolute)).ToString(); 
        }
 
        #endregion 
        //------------------------------------------------------ 
        //
        //  Private Methods
        //
        //------------------------------------------------------ 
        #region Private Methods 
 
        ///  
        /// Asserts for to get the location of the entry assembly
        ///  
        [SecurityCritical]
        private Uri GetEntryAssemblyLocation() 
        {
            Uri entryLocation = null; 
            System.Security.PermissionSet permissionSet = new PermissionSet(null); 
            permissionSet.AddPermission(new FileIOPermission(PermissionState.Unrestricted));
            permissionSet.Assert(); 
            try
            {
                entryLocation = new Uri(Application.ResourceAssembly.CodeBase);
            } 
            catch(Exception ex)
            { 
                if (CriticalExceptions.IsCriticalException(ex)) 
                {
                    throw; 
                }
                // `Swallow any other exceptions to avoid disclosing the critical path.
                //
                // Possible Exceptions: ArgumentException, ArgumentNullException, PathTooLongException 
                // DirectoryNotFoundException, IOException, UnauthorizedAccessException,
                // ArgumentOutOfRangeException, FileNotFoundException, NotSupportedException 
            } 
            finally
            { 
                CodeAccessPermission.RevertAssert();
            }
            return entryLocation;
        } 
        ///  
        /// Asserts to open the file 
        ///  
        [SecurityCritical] 
        private Stream CriticalOpenFile(string filename)
        {
            Stream s = null;
            FileIOPermission filePermission = new FileIOPermission(FileIOPermissionAccess.Read, filename); 
            filePermission.Assert();
            try 
            { 
                s = System.IO.File.Open(filename, FileMode.Open, FileAccess.Read, ResourceContainer.FileShare);
            } 
            finally
            {
                CodeAccessPermission.RevertAssert();
            } 
            return s;
        } 
 
        #endregion
 
        //-----------------------------------------------------
        //
        //  Private Fields
        // 
        //------------------------------------------------------
 
        #region Private Members 
        ///  
        /// Contains critical path information that shouldn't be disclosed.
        ///  
        [SecurityCritical]
        private string _fullPath; 
        #endregion Private Members 
    } 
}
 
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------------------------ 
//
// 
//    Copyright (C) Microsoft Corporation.  All rights reserved.
//   
//
// Description: 
// ContentFilePart is an implementation of the abstract PackagePart class. It contains an override for GetStreamCore. 
//
// History: 
//  6/16/2005: Erichar - Initial creation.
//
//-----------------------------------------------------------------------------
 
using System;
using System.IO.Packaging; 
using System.Windows; 
using System.Windows.Resources;
using System.IO; 
using System.Resources;
using System.Globalization;
using System.Security;
using System.Security.Permissions; 
using System.Windows.Navigation;
using System.Diagnostics; 
using System.Reflection; 
namespace MS.Internal.AppModel 
{
    /// 
    /// ContentFilePart is an implementation of the abstract PackagePart class. It contains an override for GetStreamCore. 
    ///  
    internal class ContentFilePart : System.IO.Packaging.PackagePart 
    { 
        //-----------------------------------------------------
        // 
        //  Public Constructors
        //
        //-----------------------------------------------------
 
        #region Public Constructors
 
        ///  
        /// Critical    -   Accesses member _fullPath.
        /// TreatAsSafe -   Initializing _fullPath to null is safe 
        ///  
        [SecurityCritical, SecurityTreatAsSafe]
        internal ContentFilePart(Package container, Uri uri) :
                base(container, uri) 
        {
            Invariant.Assert(Application.ResourceAssembly != null, "If the entry assembly is null no ContentFileParts should be created"); 
            _fullPath = null; 
        }
 
        #endregion
        //------------------------------------------------------
        // 
        //  Protected Methods
        // 
        //----------------------------------------------------- 
        #region Protected Methods 
        /// 
        /// Critical    -   Calls critical methods GetEntryAssemblyLocation() and CriticalOpenFile()
        ///                 and accesses critical member _fullPath. 
        /// TreatAsSafe -   The Uri supplied at construction is read only and must be on the list
        ///                 of loose content files supplied at application compile time.  It is ok 
        ///                 to return the stream because we know that the stream will be read only 
        ///                 and cannot be used to get the application into an invalid state.
        ///   
        [SecurityCritical, SecurityTreatAsSafe]
        protected override Stream GetStreamCore(FileMode mode, FileAccess access)
        {
            Stream stream = null; 
            if (_fullPath == null) 
            { 
                // File name will be a path relative to the applications directory.
                // - We do not want to use SiteOfOriginContainer.SiteOfOrigin because 
                //   for deployed files the  files are deployed with the application.
                Uri codeBase = GetEntryAssemblyLocation();
                string assemblyName, assemblyVersion, assemblyKey; 
                string filePath;
 
                // For now, only Application assembly supports content files, 
                // so we can simply ignore the assemblyname etc.
                // In the future, we may extend this support for regular library assembly, 
                // assemblyName will be used to predict the right file path.
                BaseUriHelper.GetAssemblyNameAndPart(Uri, out filePath, out assemblyName, out assemblyVersion, out assemblyKey);
 
                // filePath should not have leading slash.  GetAssemblyNameAndPart( ) can guarantee it.
                Uri file = new Uri(codeBase, filePath); 
                _fullPath = file.LocalPath; 
            }
 
            stream = CriticalOpenFile(_fullPath);
            if (stream == null)
            { 
                throw new IOException(SR.Get(SRID.UnableToLocateResource, Uri.ToString()));
            } 
 
            return stream;
        } 
        protected override string GetContentTypeCore()
        {
            return MS.Internal.MimeTypeMapper.GetMimeTypeFromUri(new Uri(Uri.ToString(),UriKind.RelativeOrAbsolute)).ToString(); 
        }
 
        #endregion 
        //------------------------------------------------------ 
        //
        //  Private Methods
        //
        //------------------------------------------------------ 
        #region Private Methods 
 
        ///  
        /// Asserts for to get the location of the entry assembly
        ///  
        [SecurityCritical]
        private Uri GetEntryAssemblyLocation() 
        {
            Uri entryLocation = null; 
            System.Security.PermissionSet permissionSet = new PermissionSet(null); 
            permissionSet.AddPermission(new FileIOPermission(PermissionState.Unrestricted));
            permissionSet.Assert(); 
            try
            {
                entryLocation = new Uri(Application.ResourceAssembly.CodeBase);
            } 
            catch(Exception ex)
            { 
                if (CriticalExceptions.IsCriticalException(ex)) 
                {
                    throw; 
                }
                // `Swallow any other exceptions to avoid disclosing the critical path.
                //
                // Possible Exceptions: ArgumentException, ArgumentNullException, PathTooLongException 
                // DirectoryNotFoundException, IOException, UnauthorizedAccessException,
                // ArgumentOutOfRangeException, FileNotFoundException, NotSupportedException 
            } 
            finally
            { 
                CodeAccessPermission.RevertAssert();
            }
            return entryLocation;
        } 
        ///  
        /// Asserts to open the file 
        ///  
        [SecurityCritical] 
        private Stream CriticalOpenFile(string filename)
        {
            Stream s = null;
            FileIOPermission filePermission = new FileIOPermission(FileIOPermissionAccess.Read, filename); 
            filePermission.Assert();
            try 
            { 
                s = System.IO.File.Open(filename, FileMode.Open, FileAccess.Read, ResourceContainer.FileShare);
            } 
            finally
            {
                CodeAccessPermission.RevertAssert();
            } 
            return s;
        } 
 
        #endregion
 
        //-----------------------------------------------------
        //
        //  Private Fields
        // 
        //------------------------------------------------------
 
        #region Private Members 
        ///  
        /// Contains critical path information that shouldn't be disclosed.
        ///  
        [SecurityCritical]
        private string _fullPath; 
        #endregion Private Members 
    } 
}
 
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
                          
                        
                        
                        
                    Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- ExtensionSimplifierMarkupObject.cs
- remotingproxy.cs
- NodeInfo.cs
- ExpandSegment.cs
- StateFinalizationDesigner.cs
- ImageAutomationPeer.cs
- SafeProcessHandle.cs
- HttpCookie.cs
- AnnotationHighlightLayer.cs
- TransactionManager.cs
- OutputCache.cs
- TreeNodeSelectionProcessor.cs
- MruCache.cs
- DefaultTextStoreTextComposition.cs
- PerformanceCounterPermissionEntryCollection.cs
- ResumeStoryboard.cs
- ContentType.cs
- DataFieldConverter.cs
- DescendantQuery.cs
- ImageMap.cs
- CachedBitmap.cs
- PersonalizationProviderCollection.cs
- SQLByte.cs
- Padding.cs
- SoapUnknownHeader.cs
- NonVisualControlAttribute.cs
- Crypto.cs
- InputEventArgs.cs
- FixedFlowMap.cs
- OpenTypeMethods.cs
- BinaryObjectReader.cs
- SuppressIldasmAttribute.cs
- Propagator.ExtentPlaceholderCreator.cs
- AsyncSerializedWorker.cs
- SqlCachedBuffer.cs
- EnumerableRowCollectionExtensions.cs
- WorkflowItemPresenter.cs
- InvokeAction.cs
- Material.cs
- ExternalFile.cs
- DocumentGridContextMenu.cs
- CompositeKey.cs
- CodeLinePragma.cs
- ComponentSerializationService.cs
- ZoneIdentityPermission.cs
- CssStyleCollection.cs
- HashJoinQueryOperatorEnumerator.cs
- SafeRightsManagementPubHandle.cs
- DataSourceXmlSerializer.cs
- SHA384.cs
- WebBrowser.cs
- _IPv4Address.cs
- ListItemCollection.cs
- TokenCreationParameter.cs
- ArrayWithOffset.cs
- WebConfigurationFileMap.cs
- LayoutInformation.cs
- DirectionalLight.cs
- SqlException.cs
- OracleLob.cs
- AuthenticatedStream.cs
- TransformPatternIdentifiers.cs
- OdbcFactory.cs
- DesignTimeResourceProviderFactoryAttribute.cs
- DNS.cs
- ExternalFile.cs
- DataServiceRequest.cs
- ReflectionServiceProvider.cs
- ResourceReader.cs
- CngKeyBlobFormat.cs
- PnrpPermission.cs
- DBSchemaTable.cs
- FrameworkElementFactoryMarkupObject.cs
- BitmapEffectOutputConnector.cs
- MenuEventArgs.cs
- PrintDialog.cs
- ConditionalAttribute.cs
- IOThreadScheduler.cs
- ListenerAdapter.cs
- CalloutQueueItem.cs
- Queue.cs
- StatusStrip.cs
- LinkButton.cs
- EventSource.cs
- ConstraintStruct.cs
- FormsAuthentication.cs
- InlineUIContainer.cs
- PersonalizationStateQuery.cs
- FullTextState.cs
- ErrorHandler.cs
- CorrelationExtension.cs
- NoneExcludedImageIndexConverter.cs
- ConnectionConsumerAttribute.cs
- KernelTypeValidation.cs
- CompositeFontParser.cs
- DataBindingExpressionBuilder.cs
- TextSearch.cs
- WrappedIUnknown.cs
- SubclassTypeValidator.cs
- ToolStripTextBox.cs