[英]Is it possible to create a hidden txt file in C++?
I am constructing an app in Visual Studio.我正在 Visual Studio 中构建一个应用程序。 I need to create some files to be used in a dll, but I want the files to be hidden when viewing the folder.
我需要创建一些要在 dll 中使用的文件,但我希望在查看文件夹时隐藏这些文件。 How can I do this in a C++ program?
我怎样才能在 C++ 程序中做到这一点?
Interactively, you can mark a file has hidden by right-clicking on it, selecting "Properties" and selecting "Hidden".交互式地,您可以通过右键单击文件,选择“属性”并选择“隐藏”来标记文件已隐藏。 The question is, how can do something equivalent from a C++ program?
问题是,如何从 C++ 程序中做一些等效的事情?
Use the SetFileAttributes
function in the Windows API:在 Windows API 中使用
SetFileAttributes
函数:
#include <windows.h>
#include <fstream>
std::fstream file;
int main(){
file.open("myUnhiddenFile.txt",std::ios::out);
file << "This is my unhidden file, that I have created just now" ;
file.close();
wchar_t* fileLPCWSTR = L"myUnhiddenFile.txt"; // To avoid incompatibility
// in GetFileAttributes()
int attr = GetFileAttributes(fileLPCWSTR);
if ((attr & FILE_ATTRIBUTE_HIDDEN) == 0) {
SetFileAttributes(fileLPCWSTR, attr | FILE_ATTRIBUTE_HIDDEN);
}
return(0);
}
#include <Windows.h>
DWORD attributes = GetFileAttributes("MyFile.txt");
SetFileAttributes("MyFile.txt", attributes + FILE_ATTRIBUTE_HIDDEN)
Files created with the FILE_ATTRIBUTE_HIDDEN can be easily seen when checking the " Hidden Items " checkbox.选中“ 隐藏项目”复选框时,可以轻松看到使用 FILE_ATTRIBUTE_HIDDEN 创建的文件。 The only way to really hide a file is to use a File System Filter Driver that will eliminate the file (or a file/s pattern) record from the results, whenever NtQueryDirectoryFile () is called.
真正隐藏文件的唯一方法是使用文件系统过滤器驱动程序,无论何时调用 NtQueryDirectoryFile (),它都会从结果中消除文件(或文件/s 模式)记录。 Windows File Explorer calls NtQueryDirectoryFile() so with such driver, the file won't show even if "Hidden Items" is checked.
Windows 文件资源管理器调用 NtQueryDirectoryFile(),因此使用此类驱动程序,即使选中“隐藏项目”,文件也不会显示。
Try this simple technique..试试这个简单的技术..
#include<iostream>
using namespace std;
int main(){
system("echo >file.txt");
system("attrib +h +s file.txt");
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.