doufang2228 2019-01-11 02:31
浏览 261
已采纳

如何在Windows / Mac / Linux中创建隐藏文件?

I build an console application, need create some hidden files. As well I know filename start with dot will hidden in Linux and mac, but windows?

Set file attributes?

Is there a way to create hidden files and directories in both Windows / Linux / Mac?

  • 写回答

2条回答 默认 最新

  • douchunji1885 2019-01-11 02:48
    关注

    Windows:

    SetFileAttributesW function

    Sets the attributes for a file or directory.

    FILE_ATTRIBUTE_HIDDEN 2 (0x2)

    The file or directory is hidden. It is not included in an ordinary directory listing.


    Go:

    Package syscall

    func SetFileAttributes

    func SetFileAttributes(name *uint16, attrs uint32) (err error)
    

    Convert from a Go UTF-8 encoded string (string) to a Windows UTF-16 encoded string pointer (*uint16).

    Package syscall

    func UTF16PtrFromString

    func UTF16PtrFromString(s string) (*uint16, error)
    

    UTF16PtrFromString returns pointer to the UTF-16 encoding of the UTF-8 string s, with a terminating NUL added. If s contains a NUL byte at any location, it returns (nil, EINVAL).


    Use OS Build Contraints.


    For example,

    hide/attrib.go:

    package main
    
    import (
        "fmt"
        "io/ioutil"
        "os"
    )
    
    func main() {
        filename := `test.hidden.file`
        os.Remove(filename)
        os.Remove("." + filename)
        err := ioutil.WriteFile(filename, []byte(filename), 0666)
        if err != nil {
            fmt.Fprintln(os.Stderr, err)
            return
        }
        err = HideFile(filename)
        if err != nil {
            fmt.Fprintln(os.Stderr, err)
            return
        }
        fmt.Println("hidden:", filename)
    }
    

    hide/hide.go:

    // +build !windows
    
    package main
    
    import (
        "os"
        "path/filepath"
        "strings"
    )
    
    func HideFile(filename string) error {
        if !strings.HasPrefix(filepath.Base(filename), ".") {
            err := os.Rename(filename, "."+filename)
            if err != nil {
                return err
            }
        }
        return nil
    }
    

    hide/hide_windows.go:

    // +build windows
    
    package main
    
    import (
        "syscall"
    )
    
    func HideFile(filename string) error {
        filenameW, err := syscall.UTF16PtrFromString(filename)
        if err != nil {
            return err
        }
        err = syscall.SetFileAttributes(filenameW, syscall.FILE_ATTRIBUTE_HIDDEN)
        if err != nil {
            return err
        }
        return nil
    }
    

    Output (Linux):

    $ tree hide
    hide
    ├── attrib.go
    ├── hide.go
    └── hide_windows.go
    $
    
    $ go build && ./hide
    hidden: test.hidden.file
    $ ls -a .test.hidden.file
    .test.hidden.file
    $ 
    

    Output (Windows):

    >go build && hide
    hidden: test.hidden.file
    >attrib test.hidden.file
    A   H        \test.hidden.file
    >
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。