1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Text;
- using Microsoft.Win32.SafeHandles;
- using Utilities;
- namespace DiskAccessLibrary
- {
- public class VolumeHandlePool
- {
-
- private static Dictionary<Guid, SafeFileHandle> m_handlePool = new Dictionary<Guid, SafeFileHandle>();
-
- public static SafeFileHandle ObtainHandle(Guid volumeGuid, FileAccess access, ShareMode shareMode, out bool newAllocation)
- {
- if (m_handlePool.ContainsKey(volumeGuid))
- {
- newAllocation = false;
- return m_handlePool[volumeGuid];
- }
- else
- {
- newAllocation = true;
- SafeFileHandle handle = HandleUtils.GetVolumeHandle(volumeGuid, access, shareMode);
- m_handlePool.Add(volumeGuid, handle);
- return handle;
- }
- }
- public static bool ReleaseHandle(Guid volumeGuid)
- {
- if (m_handlePool.ContainsKey(volumeGuid))
- {
- SafeFileHandle handle = m_handlePool[volumeGuid];
- if (!handle.IsClosed)
- {
- handle.Close();
- }
- m_handlePool.Remove(volumeGuid);
- return true;
- }
- else
- {
- return false;
- }
- }
- }
- }
|