Files
sbox-public/engine/Sandbox.Tools/Utility/Utility.FileDialog.cs
s&box team 71f266059a Open source release
This commit imports the C# engine code and game files, excluding C++ source code.

[Source-Commit: ceb3d758046e50faa6258bc3b658a30c97743268]
2025-11-24 09:05:18 +00:00

53 lines
1.4 KiB
C#

using System.IO;
namespace Editor;
public static partial class EditorUtility
{
/// <summary>
/// Open a file save dialog. Returns null on cancel, else the absolute path of the target file.
/// </summary>
public static string SaveFileDialog( string title, string extension, string defaultPath )
{
extension = extension.Trim( '.' );
var path = defaultPath;
if ( path.Contains( "." ) ) path = Path.GetDirectoryName( path );
var fd = new FileDialog( null );
fd.Title = title;
fd.Directory = path;
fd.DefaultSuffix = $".{extension}";
fd.SelectFile( Path.GetFileName( defaultPath ) );
fd.SetFindFile();
fd.SetModeSave();
fd.SetNameFilter( $"{extension} (*.{extension})" );
if ( !fd.Execute() )
return null;
return fd.SelectedFile;
}
/// <summary>
/// Open a file open dialog. Returns null on cancel, else the absolute path of the target file.
/// </summary>
public static string OpenFileDialog( string title, string extension, string defaultPath )
{
extension = extension.Trim( '.' );
var fd = new FileDialog( null );
fd.Title = title;
fd.Directory = Path.GetDirectoryName( defaultPath );
fd.DefaultSuffix = $".{extension}";
fd.SelectFile( Path.GetFileName( defaultPath ) );
fd.SetFindFile();
fd.SetModeOpen();
fd.SetNameFilter( $"{extension} (*.{extension})" );
if ( !fd.Execute() )
return null;
return fd.SelectedFile;
}
}