Code:
/ DotNET / DotNET / 8.0 / untmp / WIN_WINDOWS / lh_tools_devdiv_wpf / Windows / wcp / TrustUi / MS / Internal / documents / Application / FilePresentation.cs / 1 / FilePresentation.cs
//------------------------------------------------------------------------------ //// Copyright (C) Microsoft Corporation. All rights reserved. // //// Interacts with user on file based information for XpsViewer. // // // History: // 08/28/2005: [....]: Refactored some of the code from FileManagement.cs // Simplified responsiblity to user interaction //----------------------------------------------------------------------------- using System; using System.IO; using System.Security; using System.Security.Permissions; using System.Windows.Forms; using System.Windows.TrustUI; namespace MS.Internal.Documents.Application { ////// Interacts with user on file based information for XpsViewer. /// ////// Responsibility: /// Should be the only class that interacts with the user with or for file /// location information. /// internal static class FilePresentation { #region Internal Methods //------------------------------------------------------------------------- // Internal Methods //------------------------------------------------------------------------- ////// Prompts the user for the save location for the current XpsDocument. /// /// The token for the current document ////// /// Critical: /// - we access and set CriticalFileToken /// - we create a CriticalFileToken for the user specifed location /// - we access the RootBrowserWindow, which is critical /// - we assert to show the file save dialog /// - we make asserts to get the local filename specified by the /// the user; the file name is not stored /// TreatAsSafe: /// - the caller has the fileToken for the existing document /// - the data already exists on the system we are simply updating it /// with information provided by the user /// - the data is data the user already had access to /// - document is an XpsDocument a non executable extension that is /// safe to run in PartialTrust /// [SecurityCritical, SecurityTreatAsSafe] internal static bool ShowSaveFileDialog(ref CriticalFileToken fileToken) { string extension = SR.Get(SRID.FileManagementSaveExt); Trace.SafeWrite(Trace.File, "Showing SafeFileDialog."); bool result = false; SaveFileDialog save = new SaveFileDialog(); if (fileToken != null) { string originalLocation = fileToken.Location.LocalPath; new FileIOPermission(PermissionState.Unrestricted).Assert(); // BlessedAssert try { save.FileName = originalLocation; } finally { FileIOPermission.RevertAssert(); } } save.Filter = SR.Get(SRID.FileManagementSaveFilter); save.DefaultExt = extension; DialogResult dialogResult; // We need FileDialogPermissionAccess.Save and UIPermission.AllWindows // to invoke the ShowDialog method specifying a parent window. // We need to specify a parent window in order to avoid a Winforms // Common Dialog issue where the wrong window is used as the parent // which causes the Save dialog to be incorrectly localized. PermissionSet permissions = new PermissionSet(PermissionState.None); permissions.AddPermission(new UIPermission(UIPermissionWindow.AllWindows)); permissions.AddPermission(new FileDialogPermission(FileDialogPermissionAccess.Save)); // Get the root browser window, if it exists. IWin32Window rbw = null; if (DocumentApplicationDocumentViewer.Instance != null) { rbw = DocumentApplicationDocumentViewer.Instance.RootBrowserWindow; } permissions.Assert(); //BlessedAssert try { if (rbw != null) { dialogResult = save.ShowDialog(rbw); } else { dialogResult = save.ShowDialog(); } } finally { PermissionSet.RevertAssert(); } if (dialogResult == DialogResult.OK) { string filePath; // can not use OpenFile w/ stream as we need the name // to do a safe save as word does FileIOPermission getFileName = new FileIOPermission(PermissionState.None); // path is unknown at this time // get_FileName requires AllAccess; wish it was PathDiscovery imho getFileName.AllFiles = FileIOPermissionAccess.AllAccess; getFileName.Assert(); //BlessedAssert try { filePath = save.FileName; } finally { FileIOPermission.RevertAssert(); } // Add .xps extension if not already present. // This must be done manually since the file save dialog will automatically // add the extension only if the filename doesn't have a known extension. // For instance, homework.1 would become homework.1.xps, but if the user // gets up to homework.386, then the dialog would just pass it through as // is, requiring us to append the extension here. if (!extension.Equals( Path.GetExtension(filePath), StringComparison.OrdinalIgnoreCase)) { filePath = filePath + extension; } Uri file = new Uri(filePath); // as this is the only place we can verify the user authorized this // particular file we construct the token here new FileIOPermission( FileIOPermissionAccess.Read | FileIOPermissionAccess.Write, filePath) .Assert(); // BlessedAssert try { fileToken = new CriticalFileToken(file); } finally { FileIOPermission.RevertAssert(); } result = true; Trace.SafeWrite(Trace.File, "A save location was selected."); } return result; } ////// Notifies the user that the selected destination file is read-only, so /// we cannot save to that location. /// internal static void ShowDestinationIsReadOnly() { System.Windows.MessageBox.Show( SR.Get(SRID.FileManagementDestinationIsReadOnly), SR.Get(SRID.FileManagementTitleError), System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Exclamation ); } ////// Notifies the user of the failure to create temporary files, which /// prevents editing. /// internal static void ShowNoTemporaryFileAccess() { System.Windows.MessageBox.Show( SR.Get(SRID.FileManagementNoTemporaryFileAccess), SR.Get(SRID.FileManagementTitleError), System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Exclamation ); } ////// Notifies the user of the failure to open from a location. /// internal static void ShowNoAccessToSource() { System.Windows.MessageBox.Show( SR.Get(SRID.FileManagementNoAccessToSource), SR.Get(SRID.FileManagementTitleError), System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Exclamation ); } ////// Notifies the user of the failure to save to a location. /// internal static void ShowNoAccessToDestination() { System.Windows.MessageBox.Show( SR.Get(SRID.FileManagementNoAccessToDestination), SR.Get(SRID.FileManagementTitleError), System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Exclamation ); } #endregion Internal Methods } } // 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
- Rights.cs
- LocationUpdates.cs
- Substitution.cs
- FindResponse.cs
- XmlIlTypeHelper.cs
- ProviderSettingsCollection.cs
- HttpListener.cs
- StreamUpgradeBindingElement.cs
- DesignerDataSourceView.cs
- XmlAttributeHolder.cs
- ProfileService.cs
- GeometryModel3D.cs
- PanelDesigner.cs
- PEFileReader.cs
- OutputCacheProviderCollection.cs
- IgnoreFileBuildProvider.cs
- WebPartMinimizeVerb.cs
- ObjectDataSourceMethodEventArgs.cs
- GridItemProviderWrapper.cs
- ApplicationFileParser.cs
- SectionRecord.cs
- PlatformCulture.cs
- ServiceModelEnumValidator.cs
- ClientSideQueueItem.cs
- TableLayoutPanel.cs
- XmlEncoding.cs
- ScrollItemProviderWrapper.cs
- SelectorAutomationPeer.cs
- UniformGrid.cs
- EdmRelationshipRoleAttribute.cs
- EventBuilder.cs
- RequestBringIntoViewEventArgs.cs
- RequestDescription.cs
- ActivityCodeDomSerializer.cs
- _IPv4Address.cs
- DependencyPropertyAttribute.cs
- NativeRecognizer.cs
- CompilerGeneratedAttribute.cs
- TransformDescriptor.cs
- LabelLiteral.cs
- OptionalMessageQuery.cs
- Page.cs
- ConfigurationManagerInternalFactory.cs
- ISAPIRuntime.cs
- SystemIPGlobalStatistics.cs
- XPathDescendantIterator.cs
- ZoneIdentityPermission.cs
- RuntimeIdentifierPropertyAttribute.cs
- TextRenderingModeValidation.cs
- WebPartMovingEventArgs.cs
- ActivityExecutor.cs
- OraclePermissionAttribute.cs
- DebugControllerThread.cs
- StringConverter.cs
- AxHost.cs
- UserPreferenceChangingEventArgs.cs
- EqualityComparer.cs
- DataServiceRequestArgs.cs
- OdbcCommandBuilder.cs
- MonthChangedEventArgs.cs
- BamlRecords.cs
- TypeInformation.cs
- AspCompat.cs
- MouseDevice.cs
- SqlBooleanMismatchVisitor.cs
- CompoundFileStorageReference.cs
- RtType.cs
- QilLiteral.cs
- CacheAxisQuery.cs
- HtmlUtf8RawTextWriter.cs
- HtmlWindow.cs
- CommandBinding.cs
- ComponentDispatcherThread.cs
- InstallerTypeAttribute.cs
- ImageMapEventArgs.cs
- ConnectionManagementElement.cs
- StyleHelper.cs
- SendActivityDesigner.cs
- ExchangeUtilities.cs
- ProfileSection.cs
- PersistencePipeline.cs
- BinaryObjectInfo.cs
- StringAnimationUsingKeyFrames.cs
- XmlSchemaCompilationSettings.cs
- ControlPropertyNameConverter.cs
- ByteStack.cs
- XsdBuildProvider.cs
- XPathNavigator.cs
- ComponentResourceManager.cs
- BamlStream.cs
- HTMLTagNameToTypeMapper.cs
- SHA1CryptoServiceProvider.cs
- IndentedWriter.cs
- ValueConversionAttribute.cs
- SqlInternalConnection.cs
- PagesChangedEventArgs.cs
- UshortList2.cs
- LiteralControl.cs
- InputMethodStateTypeInfo.cs
- Grant.cs