문제

As far as I know, the way to create a hidden folder is:

CreateDirectory( folderName ); SetFileAttributes( folderName, FILE_ATTRIBUTE_HIDDEN );

Doing this causes the directory to exist, for a moment, as not hidden. Other programs like cloud software and backup can mistakenly see it as non-hidden... and do something with it.

Is it possible to achieve the same thing in a single API call? One atomic step? Seems like it should be possible! ??

도움이 되었습니까?

해결책

You can use CreateDirectoryEx for this.

Creates a new directory with the attributes of a specified template directory. If the underlying file system supports security on files and directories, the function applies a specified security descriptor to the new directory. The new directory retains the other attributes of the specified template directory.

You need a template directory handy with suitable (i.e. hidden) attributes.

다른 팁

Maybe these steps would help you:

  1. Create the directory outside the backup/cloud zone,
  2. make it hidden,
  3. then move it to the desired location.

Create the directory as a temp directory. GetTempPath() will give you the path to your temp directory:

DWORD WINAPI GetTempPath(
  _In_   DWORD nBufferLength,
  _Out_  LPTSTR lpBuffer
  ) ;

Use that to create a unique temp file name with GetTempFileName():

UINT WINAPI GetTempFileName(
  _In_   LPCTSTR lpPathName,
  _In_   LPCTSTR lpPrefixString,
  _In_   UINT uUnique,
  _Out_  LPTSTR lpTempFileName
  ) ;

That will, depending on how it's invoked, either

  • create an empty file with a unique name (uUnique is zero), or
  • just return the unique name (uUnique is non-zero).

Then create a directory of that name in the temp directory. When you've got it into the condition you want it WRT attributes (hidden, etc.), then move it into its final location with MoveFile() or MoveFileEx().

Of course, it might be easier just to get the temp path as above, and iteratively try to create a subdirectory, generating a unique temp name using a GUID or the current date/time and appending an incrementing suffix. Once the directory is created, then set its attributes and proceed as above to move it to its final location.

Use CreateDirectoryTransacted() and SetFileAttributesTransacted() within a single transaction that is created with CreateTransaction() and committed with CommitTransaction(), eg:

HANDLE hTrans = CreateTransaction(...);
CreateDirectoryTransacted(..., hTrans);
SetFileAttributesTransacted(..., hTrans);
CommitTransaction(hTrans);
CloseHandle(hTrans);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top