从 C 使用 Windows API 隐藏文件或目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1189832/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hide a file or directory using the Windows API from C
提问by Johannes Brodwall
I want to modify a C program to make some of the files it creates hidden in Windows. What Windows or (even better) POSIX API will set the hidden file attribute?
我想修改一个 C 程序,使其创建的一些文件隐藏在 Windows 中。什么 Windows 或(甚至更好)POSIX API 将设置隐藏文件属性?
回答by Rutger Nijlunsing
You can do it by calling SetFileAttributes and setting the FILE_ATTRIBUTE_HIDDEN flag. See http://msdn.microsoft.com/en-us/library/aa365535%28VS.85%29.aspx
您可以通过调用 SetFileAttributes 并设置 FILE_ATTRIBUTE_HIDDEN 标志来实现。请参阅http://msdn.microsoft.com/en-us/library/aa365535%28VS.85%29.aspx
This is not POSIX though. To create a 'hidden' file under a normal POSIX system like Linux, just start a filename with a dot (.).
不过,这不是 POSIX。要在 Linux 等普通 POSIX 系统下创建“隐藏”文件,只需以点 (.) 开头文件名。
回答by Joey
Windows and UNIX-like systems have different views on what exactly is a hidden file. On UNIX-likes conventionally file names starting with a dot are considered "hidden". Windows file systems on the other hand have a "hidden" attribute for files.
Windows 和类 UNIX 系统对隐藏文件的确切含义有不同的看法。在类 UNIX 上,通常以点开头的文件名被认为是“隐藏的”。另一方面,Windows 文件系统具有文件的“隐藏”属性。
So for POSIX you should probably just create your files with a starting dot in the file name.
因此,对于 POSIX,您可能应该只在文件名中使用起始点创建文件。
On Windows you can use the SetFileAttributesfunction.
在 Windows 上,您可以使用SetFileAttributes函数。
回答by Martin Beckett
Use CreateFilewith the FILE_ATTRIBUTE_HIDDEN flag
使用带有 FILE_ATTRIBUTE_HIDDEN 标志的CreateFile
回答by JaredPar
You are looking for the GetFileAttributesEx, GetFileAttributes and SetFileAttributes set of methods in the Win32 API.
您正在寻找 Win32 API 中的 GetFileAttributesEx、GetFileAttributes 和 SetFileAttributes 方法集。
Starting point of the documentation
文档的起点