在Windows 10上,无法打开Golang的下载文件夹资源管理器窗口。

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

golang open Downloads folder explorer window on Windows 10 not working

问题

我可以使用Windows 10的cmd打开资源管理器窗口。

explorer C:\Users\bruce\Downloads\

所以我想在Go中执行这个命令,但它打开的是“文档”文件夹而不是“下载”文件夹,我该如何解决这个问题?

package main

import (
	"fmt"
	"os"
	"os/exec"
	"runtime"
)

func main() {
	homeDir, _ := os.UserHomeDir()

	if runtime.GOOS == "darwin" {
		downloadDir := homeDir + "/Downloads/"
		cmd := `open "` + downloadDir + `"`
		exec.Command("/bin/bash", "-c", cmd).Output()
	} else {
		downloadDir := homeDir + "\\Downloads\\"
		cmd := `explorer ` + downloadDir
		fmt.Println("cmd: ", cmd)
		exec.Command("cmd", "/C", cmd).Output()
	}
}
英文:

I can open a explorer window using on Windows 10 cmd

explorer C:\Users\bruce\Downloads\

So I'm thinking executing this command in go, but it opens Documents folder instead of Downloads folder, how can I solve this issue

package main

import (
	"fmt"
	"os"
	"os/exec"
	"runtime"
)

func main() {
	homeDir, _ := os.UserHomeDir()

	if runtime.GOOS == "darwin" {
		downloadDir := homeDir + "/Downloads/"
		cmd := `open "` + downloadDir + `"`
		exec.Command("/bin/bash", "-c", cmd).Output()
	} else {
		downloadDir := homeDir + "\\Downloads\\"
		cmd := `explorer ` + downloadDir
		fmt.Println("cmd: ", cmd)
		exec.Command("cmd", "/C", cmd).Output()
	}
}

答案1

得分: 1

问题解决了,原来exec.Command()的第一个参数应该使用explorer而不是cmd

下面的代码片段是可行的:

package main

import (
	"os"
	"os/exec"
	"runtime"
	"strings"
)

func main() {
	homeDir, _ := os.UserHomeDir()
	downloadDir := homeDir + "/Downloads/"
	if runtime.GOOS == "darwin" {
		cmd := `open "` + downloadDir + `"`
		exec.Command("/bin/bash", "-c", cmd).Start()
	} else {
		downloadDir = strings.ReplaceAll(downloadDir, "/", "\\")
		exec.Command("explorer", downloadDir).Start()
	}
}

编辑

更简化的版本,使用downloadDir := filepath.Join(homeDir, "Downloads")代替downloadDir = strings.ReplaceAll(downloadDir, "/", "\\")

package main

import (
	"os"
	"os/exec"
	"path/filepath"
	"runtime"
)

func main() {
	homeDir, _ := os.UserHomeDir()
	downloadDir := filepath.Join(homeDir, "Downloads")
	if runtime.GOOS == "darwin" {
		exec.Command("open", downloadDir).Start()
	} else {
		exec.Command("explorer", downloadDir).Start()
	}
}

编辑2

package main

import (
	"os"
	"os/exec"
	"path/filepath"
	"runtime"
)

func main() {
	homeDir, _ := os.UserHomeDir()
	downloadDir := filepath.Join(homeDir, "Downloads")
	cmd := "open"
	if runtime.GOOS == "windows" {
		cmd = "explorer"
	}
	exec.Command(cmd, downloadDir).Start()
}

编辑3

根据@panapol-p在这里指出的内容:

package main

import (
	"os"
	"os/exec"
	"path/filepath"
	"runtime"
)

func main() {
	homeDir, _ := os.UserHomeDir()

	cmd := "open"
	downloadDir := filepath.Join(homeDir, "Downloads")
	if runtime.GOOS == "windows" {
		cmd = "explorer"
		downloadDir = "shell:downloads"
	}
	exec.Command(cmd, downloadDir).Start()
}
英文:

Problem solved, turns out that the first parameter of exec.Command() should use explorer instead of cmd

The snippet below is working

package main

import (
	"os"
	"os/exec"
	"runtime"
	"strings"
)

func main() {
	homeDir, _ := os.UserHomeDir()
	downloadDir := homeDir + "/Downloads/"
	if runtime.GOOS == "darwin" {
		cmd := `open "` + downloadDir + `"`
		exec.Command("/bin/bash", "-c", cmd).Start()
	} else {
		downloadDir = strings.ReplaceAll(downloadDir, "/", "\\")
		exec.Command("explorer", downloadDir).Start()
	}
}

Edit

More simplified version, use downloadDir := filepath.Join(homeDir, "Downloads") instead of downloadDir = strings.ReplaceAll(downloadDir, "/", "\\")

package main

import (
	"os"
	"os/exec"
	"path/filepath"
	"runtime"
)

func main() {
	homeDir, _ := os.UserHomeDir()
	downloadDir := filepath.Join(homeDir, "Downloads")
	if runtime.GOOS == "darwin" {
		exec.Command("open", downloadDir).Start()
	} else {
		exec.Command("explorer", downloadDir).Start()
	}
}

Edit2

package main

import (
	"os"
	"os/exec"
	"path/filepath"
	"runtime"
)

func main() {
	homeDir, _ := os.UserHomeDir()
	downloadDir := filepath.Join(homeDir, "Downloads")
	cmd := "open"
	if runtime.GOOS == "windows" {
		cmd = "explorer"
	}
	exec.Command(cmd, downloadDir).Start()
}

Edit3

According to @panapol-p point out here

package main

import (
	"os"
	"os/exec"
	"path/filepath"
	"runtime"
)

func main() {
	homeDir, _ := os.UserHomeDir()

	cmd := "open"
	downloadDir := filepath.Join(homeDir, "Downloads")
	if runtime.GOOS == "windows" {
		cmd = "explorer"
		downloadDir = "shell:downloads"
	}
	exec.Command(cmd, downloadDir).Start()
}

答案2

得分: 0

我建议您使用特定的文件夹,而不是自己拼接路径,因为当您将应用程序安装在自定义下载路径上时,您的应用程序将以错误的条件运行。

特定文件夹是在系统中注册的标准文件夹,适用于XP或更高版本的Windows。

对于手动操作,您可以按照以下步骤进行:

win + r -> shell:{specific folder}

例如:

win + r -> shell:downloads

对于Golang,您可以使用以下代码:

err := exec.Command("explorer", "shell:downloads").Start()
if err != nil {
    log.Fatalln(err.Error())
}

获取更多信息,请访问以下链接:
https://gist.github.com/gabe31415/ac22a4f5c874ee33ab0083fdb4e24e66

英文:

[for window]<br>
I recommend using a specific folder instead of joining the path by yourself,
because when you install the application on a custom download path, your application will be run with the wrong condition.

a specific folder is a standard folder that was registered with the system and this one is on XP or higher version of window

for manual, you can use like this
> win + r -> shell:{specific folder}

example
> win + r -> shell:downloads

for golang you can use like this

err := exec.Command(&quot;explorer&quot;, &quot;shell:downloads&quot;).Start()
if err != nil {
	log.Fatalln(err.Error())
}

for more information
https://gist.github.com/gabe31415/ac22a4f5c874ee33ab0083fdb4e24e66

huangapple
  • 本文由 发表于 2022年1月21日 15:41:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/70797904.html
匿名

发表评论

匿名网友

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

确定