Files
sbox-public/engine/Sandbox.Tools/Utility/FileAssociations.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

42 lines
1.4 KiB
C#

using Microsoft.Win32;
using System;
using System.Runtime.InteropServices;
namespace Editor;
internal class FileAssociations
{
/// <summary>
/// Creates/updates file associations between .sbproj files and the editor
/// </summary>
internal static void Create()
{
var sboxExeFilePath = $"\"{FileSystem.Root.GetFullPath( "sbox-dev.exe" )}\"";
try
{
RegistryKey fileTypeKey = Registry.CurrentUser.CreateSubKey( @"SOFTWARE\Classes\Sandbox.ProjectFile" );
fileTypeKey.SetValue( "", "Sandbox Project File" );
fileTypeKey.CreateSubKey( "DefaultIcon" ).SetValue( "", sboxExeFilePath );
RegistryKey shellKey = fileTypeKey.CreateSubKey( "shell" );
RegistryKey shellOpenKey = shellKey.CreateSubKey( "open" );
shellOpenKey.SetValue( "", "Open" );
shellOpenKey.CreateSubKey( "command" ).SetValue( "", sboxExeFilePath + " -project \"%1\"" );
RegistryKey fileExtensionKey = Registry.CurrentUser.CreateSubKey( @"SOFTWARE\Classes\.sbproj" );
fileExtensionKey.SetValue( "", "Sandbox.ProjectFile" );
// Tell explorer the file association has been changed
SHChangeNotify( 0x08000000, 0x0000, IntPtr.Zero, IntPtr.Zero );
}
catch ( Exception ex )
{
Log.Warning( ex, "Failed to associate project file extension" );
}
}
[DllImport( "shell32.dll", CharSet = CharSet.Auto, SetLastError = true )]
public static extern void SHChangeNotify( uint wEventId, uint uFlags, IntPtr dwItem1, IntPtr dwItem2 );
}