Download
file management n.
Skip this Video
Loading SlideShow in 5 Seconds..
File Management PowerPoint Presentation
Download Presentation
File Management

File Management

9 Vues Download Presentation
Télécharger la présentation

File Management

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. File Management

  2. What is File? • File • I/O device: • File • File stream • Directory • Physical disk • Volume • Console buffer • Tape drive • Communications resource • Mailslot • Pipe

  3. HANDLE WINAPI CreateFile( _In_     LPCTSTR     lpFileName, _In_     DWORD       dwDesiredAccess, _In_     DWORD       dwShareMode, _In_opt_ LPSECURITY_ATTRIBUTES lpSecurityAttributes, _In_     DWORD       dwCreationDisposition, _In_     DWORD       dwFlagsAndAttributes, _In_opt_ HANDLE   hTemplateFile);

  4. lpFileName • The name of the file or device to be created or opened. • Name can have either forward slashes (/) or backslashes (\) used.

  5. dwDesiredAccess • Requested access to the file or device: • GENERIC_READ • GENERIC_WRITE • GENERIC_EXECUTE • GENERIC_ALL • Etc. • If zero, querying some metadata available

  6. dwShareMode

  7. lpSecurityAttributes • A pointer to a SECURITY_ATTRIBUTES • Can be NULL (default security descriptor will be used) • Child process can’t inherit file handle

  8. dwCreationDisposition • What action should be taken:

  9. dwFlagsAndAttributes • FILE_ATTRIBUTE_ARCHIVE • FILE_ATTRIBUTE_ENCRYPTED • FILE_ATTRIBUTE_HIDDEN • FILE_ATTRIBUTE_NORMAL • 20 more…

  10. hTemplateFile • Handle to a template file - supplies file attributes and extended attributes

  11. Is there something simple? HANDLE WINAPI CreateFile2( _In_     LPCWSTR lpFileName, _In_     DWORD dwDesiredAccess, _In_     DWORD dwShareMode, _In_     DWORD dwCreationDisposition, _In_opt_ LPCREATEFILE2_EXTENDED_PARAMETERS pCreateExParams);

  12. Copy, transfer and renaming • CopyFile • CopyFile2 • CopyFileEx

  13. CopyFile • lpExistingFileName– pointer to string • lpNewFileName– pointer to string • bFailIfExists– if TRUE, existing file will be not overwritten

  14. CopyFileEx (1)

  15. CopyFileEx (2) • lpProgressRoutine– CALLBACK function, called after portion of file is copied • lpData– argument to be passed to the CALLBACK function (can be NULL) • pbCancel– if it becames TRUE during copy operation, operation is canceled • dwCopyFlags–specify how the file is to be copied

  16. dwCopyFlags • COPY_FILE_ALLOW_DECRYPTED_DESTINATION • COPY_FILE_FAIL_IF_EXISTS • COPY_FILE_OPEN_SOURCE_FOR_WRITE • COPY_FILE_RESTARTABLE

  17. CopyProgressRoutine (1)

  18. CopyProgressRoutine (2) • TotalFileSize – file size in bytes • TotalBytesTransferred – total transfered bytes • StreamSize – current stream size in bytes • StreamBytesTransferred – transfered bytes in stream • dwStreamNumber – handle to current stream • dwCallbackReason – reason why function was called • hSourceFile – handle to source file • hDestinationFile – handle to destination file • lpData – argument from CopyFileEx function

  19. dwCallbackReason • CALLBACK_CHUNK_FINISHED • CALLBACK_STREAM_SWITCH

  20. CopyProgressRoutine (3) • PROGRESS_CONTINUE • PROGRESS_CANCEL • PROGRESS_STOP • PROGRESS_QUIET

  21. MoveFile[Ex]

  22. dwFlags

  23. DeleteFile & RemoveDirectory

  24. ReadFile BOOL WINAPI ReadFile( _In_ HANDLE hFile, _Out_ LPVOID lpBuffer, _In_ DWORD nNumberOfBytesToRead, _Out_opt_ LPDWORD lpNumberOfBytesRead, _Inout_opt_ LPOVERLAPPED lpOverlapped);

  25. ReadFileEx BOOL WINAPI ReadFileEx( _In_ HANDLE hFile, _Out_opt_ LPVOID lpBuffer, _In_ DWORD nNumberOfBytesToRead, _Inout_ LPOVERLAPPED lpOverlapped, _In_ LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine);

  26. OVERLAPPED • Structure to contain information for asynchronous (or overlapped) I/O typedef struct _OVERLAPPED { ULONG_PTR Internal; ULONG_PTR InternalHigh; union { struct { DWORD Offset; DWORD OffsetHigh; }; PVOID  Pointer; }; HANDLE    hEvent; } OVERLAPPED, *LPOVERLAPPED;

  27. WriteFile BOOL WINAPI WriteFile( _In_ HANDLE hFile, _In_ LPCVOID lpBuffer, _In_ DWORD nNumberOfBytesToWrite, _Out_opt_ LPDWORD lpNumberOfBytesWritten, _Inout_opt_ LPOVERLAPPED lpOverlapped);

  28. WriteFileEx BOOL WINAPI WriteFileEx( _In_ HANDLE hFile, _In_opt_ LPCVOID lpBuffer, _In_ DWORD nNumberOfBytesToWrite, _Inout_ LPOVERLAPPED lpOverlapped, _In_ LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine );

  29. OpenFile • Creates, opens, reopens, or deletes a file • Obsolete (use CreateFile) HFILE WINAPI OpenFile( _In_ LPCSTR lpFileName, _Out_ LPOFSTRUCT lpReOpenBuff, _In_ UINT uStyle);

  30. Don’t forget to use • CloseHandle to release resources. • If not – some problems may occur.

  31. Other “things” There are a lot of functions to manage files. Feel free to use MSDN library.