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
- UnsafeNetInfoNativeMethods.cs
- CommandBindingCollection.cs
- DependentTransaction.cs
- SmtpSpecifiedPickupDirectoryElement.cs
- ServiceOperationDetailViewControl.cs
- HtmlShim.cs
- ErrorTableItemStyle.cs
- Panel.cs
- ToolStripScrollButton.cs
- ObjectStateFormatter.cs
- SecureConversationServiceCredential.cs
- newinstructionaction.cs
- ExplicitDiscriminatorMap.cs
- ScriptingProfileServiceSection.cs
- StringAnimationBase.cs
- DataSourceXmlAttributeAttribute.cs
- SchemaComplexType.cs
- ClientUtils.cs
- CipherData.cs
- DataSourceControl.cs
- cookieexception.cs
- SoapMessage.cs
- Int32Rect.cs
- TextModifierScope.cs
- RectValueSerializer.cs
- ObjectStateEntryDbDataRecord.cs
- BindStream.cs
- ConstraintStruct.cs
- SecurityTokenAttachmentMode.cs
- Setter.cs
- SchemaNames.cs
- ImmutableObjectAttribute.cs
- GlyphRunDrawing.cs
- XPathNavigatorReader.cs
- TextSimpleMarkerProperties.cs
- EdgeModeValidation.cs
- MailWebEventProvider.cs
- ProjectedSlot.cs
- ExpressionEditorAttribute.cs
- ContentPosition.cs
- safex509handles.cs
- MenuItemAutomationPeer.cs
- AnnotationAuthorChangedEventArgs.cs
- ToolboxBitmapAttribute.cs
- TableChangeProcessor.cs
- UnsafeNativeMethodsCLR.cs
- FormViewInsertEventArgs.cs
- GrammarBuilder.cs
- CustomErrorCollection.cs
- PtsContext.cs
- UIElement.cs
- Compress.cs
- ConnectionPool.cs
- FileController.cs
- handlecollector.cs
- PrinterUnitConvert.cs
- InfoCardTraceRecord.cs
- SecurityRuntime.cs
- RIPEMD160Managed.cs
- SiteMembershipCondition.cs
- UnsafePeerToPeerMethods.cs
- GeometryCombineModeValidation.cs
- _UncName.cs
- WindowsProgressbar.cs
- XmlExtensionFunction.cs
- XmlUtil.cs
- DocumentOrderQuery.cs
- invalidudtexception.cs
- XmlDownloadManager.cs
- XmlSchemaElement.cs
- TemplateBuilder.cs
- WorkItem.cs
- DeviceContexts.cs
- ContextMenuStrip.cs
- WorkflowDesignerMessageFilter.cs
- EmptyCollection.cs
- BitmapCodecInfoInternal.cs
- MappingItemCollection.cs
- OleDbEnumerator.cs
- HttpContext.cs
- RSAPKCS1SignatureDeformatter.cs
- URLBuilder.cs
- StringCollection.cs
- BreakRecordTable.cs
- IQueryable.cs
- EntityTypeBase.cs
- CodeAccessSecurityEngine.cs
- PowerStatus.cs
- CharacterString.cs
- EndEvent.cs
- ParserHooks.cs
- Environment.cs
- NameSpaceExtractor.cs
- MasterPageParser.cs
- QilReference.cs
- SwitchExpression.cs
- sitestring.cs
- ExpandedProjectionNode.cs
- UriScheme.cs
- TextAction.cs