Browse Source

添加windows环境下获取绝对路径方法GetCurrentPath

pull/15/head
zhangxin 4 years ago
parent
commit
8baf5e6f78
1 changed files with 28 additions and 0 deletions
  1. +28
    -0
      utils/file.go

+ 28
- 0
utils/file.go View File

@ -5,6 +5,8 @@ import (
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"strings" "strings"
"errors"
) )
func GetCurrentDir() string { func GetCurrentDir() string {
@ -95,3 +97,29 @@ func IsExists(path string) bool {
} }
return true return true
} }
//windows环境下获取绝对路径
func GetCurrentPath(dir string) (string, error) {
file, err := exec.LookPath(os.Args[0])
if err != nil {
return "", err
}
path, err := filepath.Abs(file)
if err != nil {
return "", err
}
i := strings.LastIndex(path, "/")
if i < 0 {
i = strings.LastIndex(path, "\\")
}
if i < 0 {
return "", errors.New(`error: Can't find "/" or "\".`)
}
pathdir := path[0 : i+1]
if len(dir) > 0 {
dir = strings.Replace(dir, "/", "\\", -1)
return filepath.Join(pathdir, dir), nil
}
return path[0 : i+1], nil
}

Loading…
Cancel
Save