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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With