private void LockFile() { // Lock the zipped file, other process only can ready it. UnlockFile(); _fileSafeHandle = Win32Wrapper.CreateFile(_fileName, Win32Wrapper.DESIRED_ACCESS_GENERIC_WRITE, Win32Wrapper.SHARE_MODE_READ, IntPtr.Zero, Win32Wrapper.OPEN_EXISTING, 0, IntPtr.Zero); if (_fileSafeHandle.IsInvalid) { throw new Win32Exception(Marshal.GetLastWin32Error(), _fileName);; } }
// Lock working directory so that the directory cannot be modified by other process. private void LockWorkingDirectory() { // Lock the working directory, other process CAN NOT access it. Use Win32Wrapper.SHARE_MODE_NONE, not Win32Wrapper.SHARE_MODE_READ. // This way, we prevent third-party software(like anti-virus, or delete manually by people) from accessing the document working // folder and it can not delete any files and sub directories. UnlockWorkingDirectory(); _directorySafeHandle = Win32Wrapper.CreateFile(_workingDirectory.FullName, Win32Wrapper.DESIRED_ACCESS_GENERIC_WRITE, Win32Wrapper.SHARE_MODE_NONE /*Win32Wrapper.SHARE_MODE_READ*/, IntPtr.Zero, Win32Wrapper.OPEN_EXISTING, Win32Wrapper.FILE_FLAG_BACKUP_SEMANTICS, IntPtr.Zero); if (_directorySafeHandle.IsInvalid) { throw new Win32Exception(Marshal.GetLastWin32Error(), _workingDirectory.FullName); } }