예제 #1
0
        private void initInfo()
        {
            if (ready)
            {
                return;
            }

            var root                     = RootPathName;
            var nameBufferSize           = (WinApiFS.MAX_PATH + 1) * Marshal.SystemDefaultCharSize;
            var nameCharsCount           = WinApiFS.MAX_PATH + 1;
            var ptVolumeNameBuffer       = Marshal.AllocHGlobal(nameBufferSize);
            var ptVolumeSerialNumber     = Marshal.AllocHGlobal(4);
            var ptMaximumComponentLength = Marshal.AllocHGlobal(4);
            var ptFileSystemFlags        = Marshal.AllocHGlobal(4);
            var ptFileSystemName         = Marshal.AllocHGlobal(nameBufferSize);

            try
            {
                var res = WinApiFS.GetVolumeInformation
                              (root,
                              ptVolumeNameBuffer,
                              nameCharsCount,
                              ptVolumeSerialNumber,
                              ptMaximumComponentLength,
                              ptFileSystemFlags,
                              ptFileSystemName,
                              nameCharsCount);
                if (res == 0) //not process 'device not ready (21)', 'network drive not found (53) 'path not found (3)'
                {
                    var errCode = Marshal.GetLastWin32Error();
                    if (errCode == 21)
                    {
                        DeviceReady = false;
                        ready       = true;
                    }
                    else if (errCode == 53)
                    {
                        DeviceReady = false;
                        ready       = true;
                    }
                    else if (errCode == 3)
                    {
                        DeviceReady = false;
                        ready       = true;
                    }
                    else
                    {
                        Messages.ShowException
                            (new Win32Exception(errCode),
                            string.Format
                                ("Failed get volume {0} properties.",
                                root));
                        DeviceReady = false;
                        ready       = true;
                    }
                }
                else
                {
                    DeviceReady = true;

                    RootPathName           = root;
                    VolumeName             = Marshal.PtrToStringAuto(ptVolumeNameBuffer);
                    MaximumComponentLength = Marshal.ReadInt32(ptMaximumComponentLength);
                    FileSystemFlags        = (VolumeCaps)Marshal.ReadInt32(ptFileSystemFlags);
                    FileSystemName         = Marshal.PtrToStringAuto(ptFileSystemName);

                    var serialBytes = new byte[4];
                    Marshal.Copy(ptVolumeSerialNumber, serialBytes, 0, 4);
                    SerialNumber = (uint)BitConverter.ToUInt64(serialBytes, 0);
                }

                if (DeviceReady)
                {
                    VolumeSpaceInfo = VolumeSpaceInfo.GetInfo(RootPathName);
                }
                else
                {
                    VolumeSpaceInfo = new VolumeSpaceInfo();
                }

                DriveType = WinApiFS.GetDriveType(RootPathName);
            }
            finally
            {
                Marshal.FreeHGlobal(ptFileSystemFlags);
                Marshal.FreeHGlobal(ptFileSystemName);
                Marshal.FreeHGlobal(ptMaximumComponentLength);
                Marshal.FreeHGlobal(ptVolumeNameBuffer);
                Marshal.FreeHGlobal(ptVolumeSerialNumber);

                ready = true;
            }
        }