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

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

golang open Downloads folder explorer window on Windows 10 not working

问题

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

  1. explorer C:\Users\bruce\Downloads\

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

  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "os/exec"
  6. "runtime"
  7. )
  8. func main() {
  9. homeDir, _ := os.UserHomeDir()
  10. if runtime.GOOS == "darwin" {
  11. downloadDir := homeDir + "/Downloads/"
  12. cmd := `open "` + downloadDir + `"`
  13. exec.Command("/bin/bash", "-c", cmd).Output()
  14. } else {
  15. downloadDir := homeDir + "\\Downloads\\"
  16. cmd := `explorer ` + downloadDir
  17. fmt.Println("cmd: ", cmd)
  18. exec.Command("cmd", "/C", cmd).Output()
  19. }
  20. }
英文:

I can open a explorer window using on Windows 10 cmd

  1. 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

  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "os/exec"
  6. "runtime"
  7. )
  8. func main() {
  9. homeDir, _ := os.UserHomeDir()
  10. if runtime.GOOS == "darwin" {
  11. downloadDir := homeDir + "/Downloads/"
  12. cmd := `open "` + downloadDir + `"`
  13. exec.Command("/bin/bash", "-c", cmd).Output()
  14. } else {
  15. downloadDir := homeDir + "\\Downloads\\"
  16. cmd := `explorer ` + downloadDir
  17. fmt.Println("cmd: ", cmd)
  18. exec.Command("cmd", "/C", cmd).Output()
  19. }
  20. }

答案1

得分: 1

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

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

  1. package main
  2. import (
  3. "os"
  4. "os/exec"
  5. "runtime"
  6. "strings"
  7. )
  8. func main() {
  9. homeDir, _ := os.UserHomeDir()
  10. downloadDir := homeDir + "/Downloads/"
  11. if runtime.GOOS == "darwin" {
  12. cmd := `open "` + downloadDir + `"`
  13. exec.Command("/bin/bash", "-c", cmd).Start()
  14. } else {
  15. downloadDir = strings.ReplaceAll(downloadDir, "/", "\\")
  16. exec.Command("explorer", downloadDir).Start()
  17. }
  18. }

编辑

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

  1. package main
  2. import (
  3. "os"
  4. "os/exec"
  5. "path/filepath"
  6. "runtime"
  7. )
  8. func main() {
  9. homeDir, _ := os.UserHomeDir()
  10. downloadDir := filepath.Join(homeDir, "Downloads")
  11. if runtime.GOOS == "darwin" {
  12. exec.Command("open", downloadDir).Start()
  13. } else {
  14. exec.Command("explorer", downloadDir).Start()
  15. }
  16. }

编辑2

  1. package main
  2. import (
  3. "os"
  4. "os/exec"
  5. "path/filepath"
  6. "runtime"
  7. )
  8. func main() {
  9. homeDir, _ := os.UserHomeDir()
  10. downloadDir := filepath.Join(homeDir, "Downloads")
  11. cmd := "open"
  12. if runtime.GOOS == "windows" {
  13. cmd = "explorer"
  14. }
  15. exec.Command(cmd, downloadDir).Start()
  16. }

编辑3

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

  1. package main
  2. import (
  3. "os"
  4. "os/exec"
  5. "path/filepath"
  6. "runtime"
  7. )
  8. func main() {
  9. homeDir, _ := os.UserHomeDir()
  10. cmd := "open"
  11. downloadDir := filepath.Join(homeDir, "Downloads")
  12. if runtime.GOOS == "windows" {
  13. cmd = "explorer"
  14. downloadDir = "shell:downloads"
  15. }
  16. exec.Command(cmd, downloadDir).Start()
  17. }
英文:

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

The snippet below is working

  1. package main
  2. import (
  3. "os"
  4. "os/exec"
  5. "runtime"
  6. "strings"
  7. )
  8. func main() {
  9. homeDir, _ := os.UserHomeDir()
  10. downloadDir := homeDir + "/Downloads/"
  11. if runtime.GOOS == "darwin" {
  12. cmd := `open "` + downloadDir + `"`
  13. exec.Command("/bin/bash", "-c", cmd).Start()
  14. } else {
  15. downloadDir = strings.ReplaceAll(downloadDir, "/", "\\")
  16. exec.Command("explorer", downloadDir).Start()
  17. }
  18. }

Edit

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

  1. package main
  2. import (
  3. "os"
  4. "os/exec"
  5. "path/filepath"
  6. "runtime"
  7. )
  8. func main() {
  9. homeDir, _ := os.UserHomeDir()
  10. downloadDir := filepath.Join(homeDir, "Downloads")
  11. if runtime.GOOS == "darwin" {
  12. exec.Command("open", downloadDir).Start()
  13. } else {
  14. exec.Command("explorer", downloadDir).Start()
  15. }
  16. }

Edit2

  1. package main
  2. import (
  3. "os"
  4. "os/exec"
  5. "path/filepath"
  6. "runtime"
  7. )
  8. func main() {
  9. homeDir, _ := os.UserHomeDir()
  10. downloadDir := filepath.Join(homeDir, "Downloads")
  11. cmd := "open"
  12. if runtime.GOOS == "windows" {
  13. cmd = "explorer"
  14. }
  15. exec.Command(cmd, downloadDir).Start()
  16. }

Edit3

According to @panapol-p point out here

  1. package main
  2. import (
  3. "os"
  4. "os/exec"
  5. "path/filepath"
  6. "runtime"
  7. )
  8. func main() {
  9. homeDir, _ := os.UserHomeDir()
  10. cmd := "open"
  11. downloadDir := filepath.Join(homeDir, "Downloads")
  12. if runtime.GOOS == "windows" {
  13. cmd = "explorer"
  14. downloadDir = "shell:downloads"
  15. }
  16. exec.Command(cmd, downloadDir).Start()
  17. }

答案2

得分: 0

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

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

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

win + r -> shell:{specific folder}

例如:

win + r -> shell:downloads

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

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

获取更多信息,请访问以下链接:
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

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

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:

确定