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