Click to See Complete Forum and Search --> : Prevent renaming of files


leojose
September 17th, 2005, 04:01 AM
Hi all,

In the application that I'm creating, there is a file that contains certain important information and the application will constantly refer it to get data at run-time. But the application references this file by name, and so I would like to prevent the user from renaming it. What attributes can I set to make this happen?
Are there any files in windows that actually implement this kind of rule?

Naumaan
September 17th, 2005, 05:06 AM
Try this code it will open MYFILE.TXT on C drive . Check CreateFile API from MSDN for further options.

HANDLE hFile;

hFile = CreateFile("c://MYFILE.TXT", // open MYFILE.TXT
GENERIC_READ, // open for reading
0, // no sharing
NULL, // no security
OPEN_EXISTING, // existing file only
FILE_ATTRIBUTE_NORMAL, // normal file
NULL); // no attr. template

if (hFile == INVALID_HANDLE_VALUE)
{
cout<<"Could not open file."; // process error
}

when u have opend a file then nobody can change its name untill u have closed it.

leojose
September 17th, 2005, 08:37 AM
That was a pretty neat trick, but I didn't find the following applicabale to .txt files.
Also, I would like to do the same with a virtual printer that I install on the PC. I guess it won't be possible to open the printer file and prevent somebody from changing its name. Any suggestions for this?