读取进程名称如何显示在 Golang 的 Windows 任务管理器中

Reading a process name how it is displayed in Windows Task Manager in Golang

我目前正在尝试读取 Go 中进程的名称,它在 windows 任务管理器中的表示方式。

但是,当我在 Go 中读取它时,它始终显示为可执行文件。我正在尝试了解它在任务管理器中的命名方式。

代码:

package main

import (
    "fmt"

    "github.com/shirou/gopsutil/process"
)

func main() {
    p, _ := process.Processes()
    for _, pr := range p {
        fmt.Println(pr.Name())
    }
}

这会输出所有进程名称,但 none 与此处的显示方式相匹配:

我正在尝试读取与歌曲标题一起显示的 Spotify 进程名称。

获得window称号

每次打开“Windows 任务管理器”时,它会搜索 .Net process.MainWindowTitle property, so you cant grep Windows title using gopsutil ... you have to use some Go wrapper for the .NET Core Runtime , not sure maybe this one would be helpful ... or even use C# 而不是 Golang

获取当前在 Spotify 上播放的曲目

Spotify 有 awesome docs with API usage for such case 所以你只需要生成 auth token 并将下一个 curl 请求转换为 golang fetch 函数

curl -X "GET" "https://api.spotify.com/v1/me/player/currently-playing?market=UA" -H "Accept: application/json" -H "Content-Type: application/json" -H "Authorization: Bearer <<YOUR TOKEN>>"