I am constructing an app in 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. How can I do this in a C++ program?
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?
Use the SetFileAttributes
function in the Windows API:
#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. 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. Windows File Explorer calls NtQueryDirectoryFile() so with such driver, the file won't show even if "Hidden Items" is checked.
Try this simple technique..
#include<iostream>
using namespace std;
int main(){
system("echo >file.txt");
system("attrib +h +s file.txt");
return 0;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.