path.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright 2010 The Walk Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // +build windows
  5. package walk
  6. import (
  7. "syscall"
  8. )
  9. import (
  10. "github.com/lxn/win"
  11. )
  12. func knownFolderPath(id win.CSIDL) (string, error) {
  13. var buf [win.MAX_PATH]uint16
  14. if !win.SHGetSpecialFolderPath(0, &buf[0], id, false) {
  15. return "", newError("SHGetSpecialFolderPath failed")
  16. }
  17. return syscall.UTF16ToString(buf[0:]), nil
  18. }
  19. func AppDataPath() (string, error) {
  20. return knownFolderPath(win.CSIDL_APPDATA)
  21. }
  22. func CommonAppDataPath() (string, error) {
  23. return knownFolderPath(win.CSIDL_COMMON_APPDATA)
  24. }
  25. func LocalAppDataPath() (string, error) {
  26. return knownFolderPath(win.CSIDL_LOCAL_APPDATA)
  27. }
  28. func DriveNames() ([]string, error) {
  29. bufLen := win.GetLogicalDriveStrings(0, nil)
  30. if bufLen == 0 {
  31. return nil, lastError("GetLogicalDriveStrings")
  32. }
  33. buf := make([]uint16, bufLen+1)
  34. bufLen = win.GetLogicalDriveStrings(bufLen+1, &buf[0])
  35. if bufLen == 0 {
  36. return nil, lastError("GetLogicalDriveStrings")
  37. }
  38. var names []string
  39. for i := 0; i < len(buf)-2; {
  40. name := syscall.UTF16ToString(buf[i:])
  41. names = append(names, name)
  42. i += len(name) + 1
  43. }
  44. return names, nil
  45. }