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

huangapple go评论132阅读模式
英文:

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

问题

我目前正在尝试读取在Windows任务管理器中表示的进程名称的Go代码。

然而,当我在Go中读取时,它总是显示为可执行文件。我正在尝试获取它在任务管理器中的名称。

代码:

package main

import (
	"fmt"

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

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

这将输出所有进程的名称,但没有一个与这里显示的名称匹配:

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

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

英文:

I'm currently trying to read the name of a process in Go how it is represented in the windows task manager.

However, when I read it in Go it is displayed always as the executable. I'm trying to get it how it is named in the task manager.

Code:

package main

import (
	"fmt"

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

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

This outputs all process names but none match how it is shown here:

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

I am trying to read the Spotify process name that is displayed with the song title.

答案1

得分: 0

获取窗口标题

每次打开“Windows任务管理器”时,它都会搜索.Net process.MainWindowTitle属性,所以你不能使用gopsutil来获取窗口标题...你需要使用一些针对.NET Core Runtime的Go封装,也许这个会有帮助...甚至可以使用C#而不是Golang。

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

Spotify有针对这种情况的API使用文档,所以你只需要生成授权令牌并将下一个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>>"
英文:

Get window title

Each time you open the "Windows Task Manager" it searches for .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# instead of Golang

Get currently playing track on Spotify

Spotify has awesome docs with API usage for such case
so you only need to generate auth token and convert next curl request to golang fetch function

curl -X &quot;GET&quot; &quot;https://api.spotify.com/v1/me/player/currently-playing?market=UA&quot; -H &quot;Accept: application/json&quot; -H &quot;Content-Type: application/json&quot; -H &quot;Authorization: Bearer &lt;&lt;YOUR TOKEN&gt;&gt;&quot;

huangapple
  • 本文由 发表于 2022年2月23日 16:48:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/71233804.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定