public DiskFreeSpace GetDiskFreeSpace(string devicePathName)
        {
            DiskFreeSpace space = new DiskFreeSpace()
            {
                SectorsPerCluster = 0,
                BytesPerSector    = 0,
                UserBytesFree     = 0,
                TotalBytesFree    = 0,
                TotalBytes        = 0
            };

            ErrorModes mode = SetErrorMode(ErrorModes.SEM_FAILCRITICALERRORS);

            try {
                bool successEx = GetDiskFreeSpaceEx(devicePathName, out ulong freeBytes, out ulong totalBytes, out ulong totalFreeBytes);
                if (!successEx)
                {
                    m_Win32Error = Marshal.GetLastWin32Error();
                }
                else
                {
                    space.UserBytesFree  = (long)freeBytes;
                    space.TotalBytes     = (long)totalBytes;
                    space.TotalBytesFree = (long)totalFreeBytes;
                }

                bool success = Kernel32.GetDiskFreeSpace(devicePathName, out int sectorsPerCluster, out int bytesPerSector, out int _, out int _);
                if (!success)
                {
                    m_Win32Error = Marshal.GetLastWin32Error();
                }
                else
                {
                    space.SectorsPerCluster = sectorsPerCluster;
                    space.BytesPerSector    = bytesPerSector;
                }

                if (!success && !successEx)
                {
                    return(null);
                }
                return(space);
            } finally {
                SetErrorMode(mode);
            }
        }
Exemple #2
0
        private void AddDiskFreeSpace(IDictionary <string, ResultOrError <DiskFreeSpace> > dictionary, string path, XmlElement node)
        {
            if (node == null)
            {
                return;
            }
            if (dictionary.ContainsKey(path))
            {
                return;
            }

            // Because this is a complex type, The XML will always return the default value.
            ResultOrError <DiskFreeSpace> result = GetResultOrError <DiskFreeSpace>(node);

            if (result != null)
            {
                dictionary.Add(path, result);
                return;
            }

            string        sectorsPerCluster = node["SectorsPerCluster"].Attributes["result"].Value;
            string        bytesPerSector    = node["BytesPerSector"].Attributes["result"].Value;
            string        totalBytes        = node["TotalBytes"].Attributes["result"].Value;
            string        totalBytesFree    = node["TotalBytesFree"].Attributes["result"].Value;
            string        userBytesFree     = node["UserBytesFree"].Attributes["result"].Value;
            DiskFreeSpace freeInfo          = new DiskFreeSpace()
            {
                SectorsPerCluster = int.Parse(sectorsPerCluster, CultureInfo.InvariantCulture),
                BytesPerSector    = int.Parse(bytesPerSector, CultureInfo.InvariantCulture),
                TotalBytes        = long.Parse(totalBytes, CultureInfo.InvariantCulture),
                TotalBytesFree    = long.Parse(totalBytesFree, CultureInfo.InvariantCulture),
                UserBytesFree     = long.Parse(userBytesFree, CultureInfo.InvariantCulture),
            };

            dictionary.Add(path, new ResultOrError <DiskFreeSpace>(freeInfo));
        }