diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/AppModel/ShellProvider.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/AppModel/ShellProvider.cs
index 01987ea9e74..f8d0e4aa16b 100644
--- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/AppModel/ShellProvider.cs
+++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/AppModel/ShellProvider.cs
@@ -24,12 +24,18 @@ namespace MS.Internal.AppModel
#region Structs
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
- internal struct COMDLG_FILTERSPEC
+ internal readonly struct COMDLG_FILTERSPEC
{
[MarshalAs(UnmanagedType.LPWStr)]
- public string pszName;
+ public readonly string pszName;
[MarshalAs(UnmanagedType.LPWStr)]
- public string pszSpec;
+ public readonly string pszSpec;
+
+ public COMDLG_FILTERSPEC(string name, string spec)
+ {
+ pszName = name;
+ pszSpec = spec;
+ }
}
[StructLayout(LayoutKind.Sequential, Pack = 4)]
diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/Microsoft/Win32/CommonDialog.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/Microsoft/Win32/CommonDialog.cs
index ff0b4044409..75626592812 100644
--- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/Microsoft/Win32/CommonDialog.cs
+++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/Microsoft/Win32/CommonDialog.cs
@@ -28,19 +28,8 @@ namespace Microsoft.Win32
///
/// An abstract base class for displaying common dialogs.
///
- ///
- /// InheritanceDemand for UIPermission (UIPermissionWindow.AllWindows)
- ///
public abstract class CommonDialog
{
- //---------------------------------------------------
- //
- // Constructors
- //
- //---------------------------------------------------
- //#region Constructors
- //#endregion Constructors
-
//---------------------------------------------------
//
// Public Methods
@@ -60,10 +49,7 @@ public abstract class CommonDialog
/// performs initialization tasks for all common dialogs and then
/// calls RunDialog.
///
- ///
- /// Callers must have UIPermission(UIPermissionWindow.AllWindows) to call this API.
- ///
- public virtual Nullable ShowDialog()
+ public virtual bool? ShowDialog()
{
CheckPermissionsToShowDialog();
@@ -126,10 +112,7 @@ public virtual Nullable ShowDialog()
///
/// Runs a common dialog box, with the owner as the given Window
///
- ///
- /// Callers must have UIPermission(UIPermissionWindow.AllWindows) to call this API.
- ///
- public Nullable ShowDialog(Window owner)
+ public bool? ShowDialog(Window owner)
{
CheckPermissionsToShowDialog();
@@ -200,14 +183,6 @@ public object Tag
#endregion Public Properties
- //---------------------------------------------------
- //
- // Public Events
- //
- //---------------------------------------------------
- //#region Public Events
- //#endregion Public Events
-
//---------------------------------------------------
//
// Protected Methods
@@ -331,38 +306,6 @@ private void MoveToScreenCenter(HandleRef hWnd)
#endregion Internal Methods
- //---------------------------------------------------
- //
- // Internal Properties
- //
- //---------------------------------------------------
- //#region Internal Properties
- //#endregion Internal Properties
-
- //---------------------------------------------------
- //
- // Internal Events
- //
- //---------------------------------------------------
- //#region Internal Events
- //#endregion Internal Events
-
- //---------------------------------------------------
- //
- // Private Methods
- //
- //---------------------------------------------------
- //#region Private Methods
- //#endregion Private Methods
-
- //---------------------------------------------------
- //
- // Protected Properties
- //
- //---------------------------------------------------
- //#region Protected Properties
- //#endregion Protected Properties
-
//---------------------------------------------------
//
// Private Fields
@@ -373,7 +316,7 @@ private void MoveToScreenCenter(HandleRef hWnd)
// Private variable used to store data for the Tag property
private object _userData;
- private Thread _thread = Thread.CurrentThread;
+ private readonly Thread _thread = Thread.CurrentThread;
///
/// The owner hwnd passed into the dialog is stored as a private
diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/Microsoft/Win32/CommonItemDialog.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/Microsoft/Win32/CommonItemDialog.cs
index 5d1daaee60c..aa0a7025867 100644
--- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/Microsoft/Win32/CommonItemDialog.cs
+++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/Microsoft/Win32/CommonItemDialog.cs
@@ -500,14 +500,6 @@ private protected string[] CloneItemNames()
#endregion Internal Properties
- //---------------------------------------------------
- //
- // Internal Events
- //
- //---------------------------------------------------
- //#region Internal Events
- //#endregion Internal Events
-
//---------------------------------------------------
//
// Private Methods
@@ -697,10 +689,10 @@ private protected sealed class VistaDialogEvents : IFileDialogEvents, IDisposabl
{
public delegate bool OnOkCallback(IFileDialog dialog);
- private IFileDialog _dialog;
+ private readonly IFileDialog _dialog;
- private OnOkCallback _okCallback;
- uint _eventCookie;
+ private readonly OnOkCallback _okCallback;
+ private readonly uint _eventCookie;
public VistaDialogEvents(IFileDialog dialog, OnOkCallback okCallback)
{
diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/Microsoft/Win32/FileDialog.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/Microsoft/Win32/FileDialog.cs
index ca5e661559f..5d907debbc7 100644
--- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/Microsoft/Win32/FileDialog.cs
+++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/Microsoft/Win32/FileDialog.cs
@@ -2,13 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
-using MS.Internal;
-using MS.Internal.AppModel;
-using MS.Internal.Interop;
-using System.ComponentModel;
-using System.IO;
-using System.Windows;
-
//
//
// Description:
@@ -19,9 +12,17 @@
// deferred to the derived classes.
//
+using System.Runtime.InteropServices;
+using System.ComponentModel;
+using MS.Internal.AppModel;
+using MS.Internal.Interop;
+using System.Windows;
+using MS.Internal;
+using System.IO;
namespace Microsoft.Win32
{
+
///
/// Provides a common base class for wrappers around both the
/// File Open and File Save common dialog boxes. Derives from
@@ -181,7 +182,7 @@ public string FileName
{
// UNDONE : This broke the save file dialog.
//string temp = Path.GetFullPath(value); // ensure filename is valid...
- MutableItemNames = new string[] { value };
+ MutableItemNames = [value];
}
}
}
@@ -269,9 +270,7 @@ public string DefaultExt
{
if (value != null)
{
- // Use Ordinal here as per FxCop CA1307
- if (value.StartsWith(".", StringComparison.Ordinal)) // Allow calling code to provide
- // extensions like ".ext" -
+ if (value.StartsWith('.')) // Allow calling code to provide extensions like ".ext" -
{
value = value.Substring(1); // but strip out the period to leave only "ext"
}
@@ -298,9 +297,6 @@ public string DefaultExt
/// Thrown in the setter if the new filter string does not have an even number of tokens
/// separated by the vertical bar character '|' (that is, the new filter string is invalid.)
///
- ///
- /// Callers must have FileIOPermission(PermissionState.Unrestricted) to call this API.
- ///
public string Filter
{
get
@@ -312,23 +308,22 @@ public string Filter
set
{
- if (String.CompareOrdinal(value, _filter) != 0) // different filter than what we have stored already
+ if (!string.Equals(value, _filter, StringComparison.Ordinal)) // different filter than what we have stored already
{
string updatedFilter = value;
- if (!String.IsNullOrEmpty(updatedFilter))
+ if (!string.IsNullOrEmpty(updatedFilter))
{
// Require the number of segments of the filter string to be even -
// in other words, there must only be matched pairs of description and
// file extensions.
//
// This implicitly requires there to be at least one vertical bar in
- // the filter string - or else formats.Length will be 1, resulting in an
+ // the filter string - or else formatsCount will be 1, resulting in an
// ArgumentException.
+ int formatsCount = updatedFilter.AsSpan().Count('|');
- string[] formats = updatedFilter.Split('|');
-
- if (formats.Length % 2 != 0)
+ if (formatsCount % 2 == 0)
{
throw new ArgumentException(SR.FileDialogInvalidFilter);
}
@@ -406,14 +401,6 @@ public bool RestoreDirectory
#endregion Public Events
- //---------------------------------------------------
- //
- // Public Events
- //
- //---------------------------------------------------
- // #region Public Events
- // #endregion Public Events
-
//---------------------------------------------------
//
// Protected Methods
@@ -426,10 +413,7 @@ public bool RestoreDirectory
///
protected override void OnItemOk(CancelEventArgs e)
{
- if (FileOk != null)
- {
- FileOk(this, e);
- }
+ FileOk?.Invoke(this, e);
}
#endregion Protected Methods
@@ -525,22 +509,6 @@ private protected override void RevertItemOk(object state)
#endregion
- //---------------------------------------------------
- //
- // Internal Properties
- //
- //---------------------------------------------------
- //#region Internal Properties
- //#endregion Internal Properties
-
- //---------------------------------------------------
- //
- // Internal Events
- //
- //---------------------------------------------------
- //#region Internal Events
- //#endregion Internal Events
-
//---------------------------------------------------
//
// Private Methods
@@ -584,7 +552,7 @@ private bool ProcessFileNames()
// a list of valid extensions from the filter(s).
// The first extension from FilterExtensions is the
// default extension.
- string[] extensions = GetFilterExtensions();
+ ReadOnlySpan extensions = GetFilterExtensions();
// For each filename:
// - Process AddExtension
@@ -597,19 +565,17 @@ private bool ProcessFileNames()
if (AddExtension && !Path.HasExtension(fileName))
{
// Loop through all extensions, starting with the default extension
- for (int j = 0; j < extensions.Length; j++)
+ foreach (string extension in extensions)
{
// Assert for a valid extension
- Invariant.Assert(!extensions[j].StartsWith(".", StringComparison.Ordinal),
- "FileDialog.GetFilterExtensions should not return things starting with '.'");
+ Invariant.Assert(!extension.StartsWith('.'), "FileDialog.GetFilterExtensions should not return things starting with '.'");
string currentExtension = Path.GetExtension(fileName);
// Assert to make sure Path.GetExtension behaves as we think it should, returning
// "" if the string is empty and something beginnign with . otherwise.
- // Use StringComparison.Ordinal as per FxCop CA1307 and CA130.
- Invariant.Assert(currentExtension.Length == 0 || currentExtension.StartsWith(".", StringComparison.Ordinal),
- "Path.GetExtension should return something that starts with '.'");
+ Invariant.Assert(currentExtension.Length == 0 || currentExtension.StartsWith('.'),
+ "Path.GetExtension should return something that starts with '.'");
// Because we check Path.HasExtension above, files should
// theoretically not have extensions at this stage - but
@@ -620,14 +586,14 @@ private bool ProcessFileNames()
// of the filename in s.
string newFilename;
- if (((ReadOnlySpan)extensions[j]).IndexOfAny('*', '?') != -1)
+ if (extension.AsSpan().ContainsAny('*', '?'))
{
// we don't want to append the extension if it contains wild cards
newFilename = fileName.Substring(0, fileName.Length - currentExtension.Length);
}
else
{
- newFilename = $"{fileName.AsSpan(0, fileName.Length - currentExtension.Length)}.{extensions[j]}";
+ newFilename = $"{fileName.AsSpan(0, fileName.Length - currentExtension.Length)}.{extension}";
}
// If FOS_FILEMUSTEXIST is not set, or if it is set but the filename we generated
@@ -670,25 +636,22 @@ private static COMDLG_FILTERSPEC[] GetFilterItems(string filter)
{
// Expecting pipe delimited filter string pairs.
// First is the label, second is semi-colon delimited list of extensions.
- var extensions = new List();
+ COMDLG_FILTERSPEC[] extensions = null;
if (!string.IsNullOrEmpty(filter))
{
string[] tokens = filter.Split('|');
- if (0 == tokens.Length % 2)
+ if (tokens.Length % 2 == 0)
{
- for (int i = 1; i < tokens.Length; i += 2)
+ extensions = new COMDLG_FILTERSPEC[tokens.Length / 2];
+ for (int i = 0; i < extensions.Length; i++)
{
- extensions.Add(
- new COMDLG_FILTERSPEC
- {
- pszName = tokens[i - 1],
- pszSpec = tokens[i],
- });
+ extensions[i] = new(tokens[i * 2], tokens[i * 2 + 1]);
}
}
}
- return extensions.ToArray();
+
+ return extensions ?? Array.Empty();
}
#endregion Private Methods
@@ -708,10 +671,10 @@ private static COMDLG_FILTERSPEC[] GetFilterItems(string filter)
///
/// Thrown if the filter string stored in the dialog is invalid.
///
- private string[] GetFilterExtensions()
+ private ReadOnlySpan GetFilterExtensions()
{
- string filter = this._filter;
- List extensions = new List();
+ string filter = _filter;
+ List extensions = new();
// Always make the default extension the first in the list,
// because other functions process files in order accepting the first
@@ -756,27 +719,27 @@ private string[] GetFilterExtensions()
{
// Find our filter in the tokens list, then split it on the
// ';' character (which is the filter extension delimiter)
- string[] exts = tokens[indexOfExtension].Split(';');
+ ReadOnlySpan exts = tokens[indexOfExtension].AsSpan();
- foreach (string ext in exts)
+ foreach (Range ext in exts.Split(';'))
{
// Filter extensions should be in the form *.txt or .txt,
// so we strip out everything before and including the '.'
// before adding the extension to our list.
// If the extension has no '.', we just ignore it as invalid.
- int i = ext.LastIndexOf('.');
+ int i = exts[ext].LastIndexOf('.');
if (i >= 0)
{
- // start the substring one beyond the location of the '.'
+ // start the slice one beyond the location of the '.'
// (i) and continue to the end of the string
- extensions.Add(ext.Substring(i + 1, ext.Length - (i + 1)));
+ extensions.Add(exts[ext].Slice(i + 1).ToString());
}
}
}
}
- return extensions.ToArray();
+ return CollectionsMarshal.AsSpan(extensions);
}
#endregion Private Properties
diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/Microsoft/Win32/OpenFileDialog.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/Microsoft/Win32/OpenFileDialog.cs
index f9c60fe0c46..10526c558ea 100644
--- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/Microsoft/Win32/OpenFileDialog.cs
+++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/Microsoft/Win32/OpenFileDialog.cs
@@ -1,4 +1,4 @@
-// Licensed to the .NET Foundation under one or more agreements.
+// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
@@ -61,9 +61,6 @@ public OpenFileDialog() : base()
///
/// Thrown if there are no filenames stored in the OpenFileDialog.
///
- ///
- /// Callers must have FileDialogPermission(FileDialogPermissionAccess.Open) to call this API.
- ///
public Stream OpenFile()
{
string filename = CriticalItemName;
@@ -85,9 +82,6 @@ public Stream OpenFile()
///
/// Thrown if there are no filenames stored in the OpenFileDialog
///
- ///
- /// Callers must have FileDialogPermission(FileDialogPermissionAccess.Open) to call this API.
- ///
public Stream[] OpenFiles()
{
// Cache ItemNames to avoid perf issues as per
@@ -124,9 +118,6 @@ public Stream[] OpenFiles()
///
/// Resets all properties to their default values.
///
- ///
- /// Callers must have FileIOPermission(PermissionState.Unrestricted) to call this API.
- ///
public override void Reset()
{
@@ -204,22 +195,6 @@ public bool Multiselect
#endregion Public Properties
- //---------------------------------------------------
- //
- // Public Events
- //
- //---------------------------------------------------
- //#region Public Events
- //#endregion Public Events
-
- //---------------------------------------------------
- //
- // Protected Methods
- //
- //---------------------------------------------------
- // #region Protected Methods
- // #endregion Protected Methods
-
//---------------------------------------------------
//
// Internal Methods
@@ -234,22 +209,6 @@ private protected override IFileDialog CreateDialog()
#endregion Internal Methods
- //---------------------------------------------------
- //
- // Internal Properties
- //
- //---------------------------------------------------
- //#region Internal Properties
- //#endregion Internal Properties
-
- //---------------------------------------------------
- //
- // Internal Events
- //
- //---------------------------------------------------
- //#region Internal Events
- //#endregion Internal Events
-
//---------------------------------------------------
//
// Private Methods
@@ -277,21 +236,5 @@ private void Initialize()
}
#endregion Private Methods
-
- //---------------------------------------------------
- //
- // Private Properties
- //
- //---------------------------------------------------
- //#region Private Properties
- //#endregion Private Properties
-
- //---------------------------------------------------
- //
- // Private Fields
- //
- //---------------------------------------------------
- //#region Private Fields
- //#endregion Private Fields
}
}
diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/Microsoft/Win32/OpenFolderDialog.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/Microsoft/Win32/OpenFolderDialog.cs
index 36a88c43a68..14de05150f2 100644
--- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/Microsoft/Win32/OpenFolderDialog.cs
+++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/Microsoft/Win32/OpenFolderDialog.cs
@@ -1,13 +1,7 @@
-// Licensed to the .NET Foundation under one or more agreements.
+// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
-using System.ComponentModel;
-using System.IO;
-
-using MS.Internal.AppModel;
-using MS.Internal.Interop;
-
//
//
// Description:
@@ -16,6 +10,11 @@
// additional properties relevant only to folder open dialog.
//
+using System.ComponentModel;
+using MS.Internal.AppModel;
+using MS.Internal.Interop;
+using System.IO;
+
namespace Microsoft.Win32
{
///
@@ -57,9 +56,6 @@ public OpenFolderDialog() : base()
///
/// Resets all properties to their default values.
///
- ///
- /// Callers must have FileIOPermission(PermissionState.Unrestricted) to call this API.
- ///
public override void Reset()
{
base.Reset();
@@ -225,14 +221,6 @@ public bool Multiselect
#endregion Public Events
- //---------------------------------------------------
- //
- // Public Events
- //
- //---------------------------------------------------
- //#region Public Events
- //#endregion Public Events
-
//---------------------------------------------------
//
// Protected Methods
@@ -264,22 +252,6 @@ private protected override IFileDialog CreateDialog()
#endregion Internal Methods
- //---------------------------------------------------
- //
- // Internal Properties
- //
- //---------------------------------------------------
- //#region Internal Properties
- //#endregion Internal Properties
-
- //---------------------------------------------------
- //
- // Internal Events
- //
- //---------------------------------------------------
- //#region Internal Events
- //#endregion Internal Events
-
//---------------------------------------------------
//
// Private Methods
@@ -309,21 +281,5 @@ private void Initialize()
}
#endregion Private Methods
-
- //---------------------------------------------------
- //
- // Private Properties
- //
- //---------------------------------------------------
- //#region Private Properties
- //#endregion Private Properties
-
- //---------------------------------------------------
- //
- // Private Fields
- //
- //---------------------------------------------------
- //#region Private Fields
- //#endregion Private Fields
}
}
diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/Microsoft/Win32/SaveFileDialog.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/Microsoft/Win32/SaveFileDialog.cs
index 70884c5c9c6..286b7735a90 100644
--- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/Microsoft/Win32/SaveFileDialog.cs
+++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/Microsoft/Win32/SaveFileDialog.cs
@@ -1,4 +1,4 @@
-// Licensed to the .NET Foundation under one or more agreements.
+// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
@@ -60,9 +60,6 @@ public SaveFileDialog() : base()
///
/// Thrown if there are no filenames stored in the SaveFileDialog.
///
- ///
- /// Callers must have UIPermission.AllWindows to call this API.
- ///
public Stream OpenFile()
{
@@ -87,9 +84,6 @@ public Stream OpenFile()
///
/// Resets all properties to their default values.
///
- ///
- /// Callers must have UIPermission.AllWindows to call this API.
- ///
public override void Reset()
{
@@ -124,9 +118,6 @@ public override void Reset()
/// Gets or sets a value indicating whether the dialog box prompts the user for
/// permission to create a file if the user specifies a file that does not exist.
///
- ///
- /// Callers must have UIPermission.AllWindows to call this API.
- ///
public bool CreatePrompt { get; set; }
///
@@ -156,29 +147,10 @@ public bool CreateTestFile
/// Gets or sets a value indicating whether the Save As dialog box displays a
/// warning if the user specifies a file name that already exists.
///
- ///
- /// Callers must have UIPermission.AllWindows to call this API.
- ///
public bool OverwritePrompt { get; set; }
#endregion Public Properties
- //---------------------------------------------------
- //
- // Public Events
- //
- //---------------------------------------------------
- //#region Public Events
- //#endregion Public Events
-
- //---------------------------------------------------
- //
- // Protected Methods
- //
- //---------------------------------------------------
- //#region Protected Methods
- //#endregion Protected Methods
-
//---------------------------------------------------
//
// Internal Methods
@@ -250,22 +222,6 @@ private protected override IFileDialog CreateDialog()
#endregion Internal Methods
- //---------------------------------------------------
- //
- // Internal Properties
- //
- //---------------------------------------------------
- //#region Internal Properties
- //#endregion Internal Properties
-
- //---------------------------------------------------
- //
- // Internal Events
- //
- //---------------------------------------------------
- //#region Internal Events
- //#endregion Internal Events
-
//---------------------------------------------------
//
// Private Methods
@@ -314,21 +270,5 @@ private bool PromptFileOverwrite(string fileName)
}
#endregion Private Methods
-
- //---------------------------------------------------
- //
- // Private Properties
- //
- //---------------------------------------------------
- //#region Private Properties
- //#endregion Private Properties
-
- //---------------------------------------------------
- //
- // Private Fields
- //
- //---------------------------------------------------
- //#region Private Fields
- //#endregion Private Fields
}
}