Go生产exe后,读取目录有明显差异

首先贴上我的代码:`

funcdealWebdir(confreadfile.Config,pathstring,webfile[]readfile.Ruledata,webfile_white[]readfile.Ruledata) {
    files, _ := ioutil.ReadDir(path)
    skip_slice := strings.Split(conf.Webfile_skip,"|")	
    var stat int = 0
   for _, file := range files {
    fmt.Println(file.Name())
       if file.IsDir() {
     	fmt.Println(file.Name())
        dealWebdir(conf,path+"/" + file.Name(),webfile,webfile_white)
    } else {
        //对文件进行处理
        //fmt.Println(myfolder + "/" + file.Name(),"mode:",file.Mode(),"size:",file.Size(),"modetime:",file.ModTime)
		for _,value := range skip_slice{
			if strings.HasSuffix(strings.ToUpper(file.Name()), strings.ToUpper(value)){
				stat = 1
			}
		}
		if stat == 0{
			dealWebfile(conf,file,path,webfile,webfile_white)
		}else{
			//fmt.Println("path:",path,"skip--",file.Name())
			stat = 0
		}
	}
}

}

参数可以可以忽略,只要关心path就可以。现在的问题是这样的,当我生成exe文件后,把exe放到f:/目录里面,当我path是F:的时候只能读到exe所在的目录。我读D盘没有问题。C盘,也会出现异常。恳请大神能够指点一下。

Do not ignore the errors. Check the error from ioutil.ReadDir and see what it is. If your path is relative, it might matter where the exe is or how it’s run. If the error doesn’t make sense, we can perhaps help.

1 Like

您好,首先很抱歉我的英文不是很好。我再详细的阐述一下具体的情况:
C:------XXX
-----XXX

D:-----XXXX
-----XXXXX

F:----JOS-----
----bin—operator.exe
----xxx

以上是我大体的目录结构,当我运行exe文件时,如果传入的路径是F:那么会出现结果读到的直接是exe文件。若果是f:/xxx则可以正常访问xxx目录。c:会涉及到文件权限,则出现一些问题。D:可以正常使用。

请问,是不是环境变量的问题,导致读取F盘的时候,异常。

期待您的回复

Do not ignore the errors. Check the error from ioutil.ReadDir and see what it is. Tell us what it is.


错误是nil

错误是nil

So tell us what actual error you do see.

Haven’t you said, F: works without problems anyway? What is it you see when trying to read C:s or D:s content?

当我go 生成的exe文件放到F:的某个目录下的时候,如果我输入的路径是F:,读取的目录是直接默认到exe所在的目录。如果我输入的路径是F:盘下面的某个确定目录,所得到的结果便是正常的。以上这些都是在不报错的情况下。我认为可能是环境变量有影响。

当我go 生成的exe文件放到F:的某个目录下的时候,如果我输入的路径是F:,读取的目录是直接默认到exe所在的目录。如果我输入的路径是F:盘下面的某个确定目录,所得到的结果便是正常的。以上这些都是在不报错的情况下。我认为可能是环境变量有影响。

对于别的磁盘是正常遍历目录的。

I do not understand. What does happen if you try C:\ as path?

,是不是权限的问题

I’m not sure about the folder application data, but many of the default folders of a windows installation are heavily restricted. Try one you know even a guest account should be able to read.

Edit

Also please copy and paste the output rather than screenshotting it. Screenshots are hard to follow on my mobile with reduced data plan due to reaching quota very early this month…

But what I’d like to know most about F: disk is that it reads directly to the directory where the EXE file resides, rather than traversing the F: directory as a whole.