You can do this through the PlaySound API through winmm.dll, in Windows Vista or higher. Microsoft has added a separate session for System Sounds, which can be used even from services by simply adding a flag.
I formatted it correctly to avoid problems with the C # 2017 IDE by throwing shaky over DllImport without being in a class named "NativeMethods".
using System.Runtime.InteropServices; namespace Audio { internal static class NativeMethods { [DllImport("winmm.dll", EntryPoint = "PlaySound", SetLastError = true, CharSet = CharSet.Unicode, ThrowOnUnmappableChar = true)] public static extern bool PlaySound( string szSound, System.IntPtr hMod, PlaySoundFlags flags); [System.Flags] public enum PlaySoundFlags : int { SND_SYNC = 0x0000, SND_ASYNC = 0x0001, SND_NODEFAULT = 0x0002, SND_MEMORY = 0x0004, SND_LOOP = 0x0008, SND_NOSTOP = 0x0010, SND_NOWAIT = 0x00002000, SND_ALIAS = 0x00010000, SND_ALIAS_ID = 0x00110000, SND_FILENAME = 0x00020000, SND_RESOURCE = 0x00040004, SND_PURGE = 0x0040, SND_APPLICATION = 0x0080, SND_SENTRY = 0x00080000, SND_RING = 0x00100000, SND_SYSTEM = 0x00200000 } } public static class Play { public static void PlaySound(string path, string file = "") { NativeMethods.PlaySound(path + file, new System.IntPtr(), NativeMethods.PlaySoundFlags.SND_ASYNC | NativeMethods.PlaySoundFlags.SND_SYSTEM); } } }
source share