Reputation: 509
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?
Upvotes: 3
Views: 13859
Reputation: 1
Try this simple technique..
#include<iostream>
using namespace std;
int main(){
system("echo >file.txt");
system("attrib +h +s file.txt");
return 0;
}
Upvotes: 0
Reputation: 4245
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.
Upvotes: 0
Reputation: 17289
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);
}
Upvotes: 13
Reputation: 525
#include <Windows.h>
DWORD attributes = GetFileAttributes("MyFile.txt");
SetFileAttributes("MyFile.txt", attributes + FILE_ATTRIBUTE_HIDDEN)
Upvotes: 4