3

For some reasons, I want to play a system sound which is named "EmptyRecycleBin" in my program when a file has been deleted.

So I wrote these code:

Code1

[DllImport("winmm.dll", EntryPoint = "sndPlaySound")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern Boolean SndPlaySound(String pszSound, Int32 fuSound);
private const Int32 SND_SYSTEM = 0x00200000;

SndPlaySound("EmptyRecycleBin", SND_SYSTEM );  //The sound that was played is wrong, but I don't know why……

sndPlaySound function executes successfully, but unfortunately, the sound that was played is "Beep".

According to @AlexK. 's explain, sndPlaySound function can't identify the alias "EmptyRecycleBin".

In this case, I have to do this in another way:

Code2

 var sound = new SoundPlayer(Registry.CurrentUser.OpenSubKey(@"AppEvents\Schemes\Apps\Explorer\EmptyRecycleBin\.Current", false).GetValue(null) as String);
 sound.PlaySync();

It's all ok, but I still want to know that if it's possible to make Code1 work by using any other alias?

My system is windows 10, any help would be appreciated.

  • 1
    Its not playing the wrong sound its playing the default sound as it can't locate EmptyRecycleBin (see snd_nodefault). FWIW this code does not work for me either, it does with some other monikers like "DeviceConnect". Process monitor shows that it does not look in that particular \Explorer subkey. – Alex K. Sep 17 '17 at 14:08
  • @AlexK. Thanks a lot, I will modify my question. – differentrain Sep 17 '17 at 14:25
  • 1
    `HKEY_CURRENT_USER\AppEvents\Schemes\Apps\.Default` registry key I think. Using SND_NODEFAULT is important so you won't play a sound that the user does not want to hear. This is configured by the user through Control Panel > Sounds > Sounds tab. I can't find the equivalent of "EmptyRecycleBin" back in the list, looks like an oversight. – Hans Passant Sep 17 '17 at 15:07
  • 1
    As documented, ["`sndPlaySound` offers a subset of the functionality of the `PlaySound` function"](https://msdn.microsoft.com/en-us/library/windows/desktop/dd798676(v=vs.85).aspx). As [documented](https://msdn.microsoft.com/en-us/library/windows/desktop/dd743680(v=vs.85).aspx), when using a system sound alias with `PlaySound`, it must be one one of the: `SystemAsterisk`, `SystemDefault`, `SystemExclamation`, `SystemExit`, `SystemHand`, `SystemQuestion`, `SystemStart`, `SystemWelcome`. – GSerg Sep 17 '17 at 15:53
  • Thats true for SND_ALIAS_ID but any valid string is ok with SND_ALIAS which is how sndPlaySound behaves. – Alex K. Sep 17 '17 at 16:19

0 Answers0