這就是我目前的解決方法。這很糟糕,我知道。它找不到當前的活動設備,而是將信息存儲在文本文件中(必須已經存在!使用當前設備名稱!)。是的,這太可怕了。但考慮到我對VBScript的瞭解以及Windows註冊表的最新知識,兩者都非常接近零,這是我能想到的最好的!
我發佈這裏,因爲缺乏一個簡單的答案在其他地方。如果有人有更好的解決方案,不用任何其他程序,我會非常感激知道。
此外,如果有人想使用此代碼,請更改文件路徑和名稱以適合您的。
Dim objFso, objFileHandle, strDisplayString
Set objFso = WScript.CreateObject("Scripting.FileSystemObject")
Set readObjFileHandle = objFso.OpenTextFile("L:\MyApps\NirCmd\CurrentDevice.txt", 1)
strDisplayString = readObjFileHandle.ReadLine()
readObjFileHandle.Close
Set writeObjFileHandle = objFso.OpenTextFile("L:\MyApps\NirCmd\CurrentDevice.txt", 2, "True")
If StrComp(strDisplayString, "Headphones", vbTextCompare) = 0 Then
'MsgBox "Headphones - switching to Speakers"
Set WshShell = CreateObject("WScript.Shell")
cmds=WshShell.RUN("L:\MyApps\NirCmd\nircmd.exe setdefaultsounddevice ""Speakers""", 2, True)
Set WshShell = Nothing
writeObjFileHandle.Write("Speakers")
Else
'MsgBox "Speakers - switching to Headphones"
Set WshShell = CreateObject("WScript.Shell")
cmds=WshShell.RUN("L:\MyApps\NirCmd\nircmd.exe setdefaultsounddevice ""Headphones""", 2, True)
Set WshShell = Nothing
writeObjFileHandle.Write("Headphones")
End If
writeObjFileHandle.Close