मुफ्त डिस्क स्थान प्राप्त करें


93

नीचे दिए गए प्रत्येक इनपुट को देखते हुए, मैं उस स्थान पर निःशुल्क स्थान प्राप्त करना चाहूंगा। कुछ इस तरह

long GetFreeSpace(string path)

इनपुट:

c:

c:\

c:\temp

\\server

\\server\C\storage

सटीक डुप्लिकेट: stackoverflow.com/questions/412632
dtb

52
डुप्लिकेट नहीं, stackoverflow.com/questions/412632 केवल डिस्क के बारे में पूछता है, मैं UNC पथों के बारे में भी पूछता हूं और 412632 में समाधान उनके लिए काम नहीं करता है।
bh213

जवाबों:


67

यह मेरे लिए काम करता है ...

using System.IO;

private long GetTotalFreeSpace(string driveName)
{
    foreach (DriveInfo drive in DriveInfo.GetDrives())
    {
        if (drive.IsReady && drive.Name == driveName)
        {
            return drive.TotalFreeSpace;
        }
    }
    return -1;
}

सौभाग्य!


7
drive.TotalFreeSpaceमेरे लिए नहीं काम करता है, लेकिन drive.AvailableFreeSpaceकरता है
knocte

16
मुझे पता है कि यह उत्तर प्राचीन है, लेकिन आपको आमतौर पर AvailableFreeSpace@knocte के अनुसार उपयोग करने की आवश्यकता है । AvailableFreeSpaceयह सूचीबद्ध करता है कि उपयोगकर्ता के लिए वास्तव में कितना उपलब्ध है (उद्धरण के कारण)। TotalFreeSpaceडिस्क पर उपलब्ध सूची को सूचीबद्ध करता है, भले ही उपयोगकर्ता क्या उपयोग कर सकता है।
रॉय टी।

मैंने @ रॉयटी की टिप्पणी को गलत ठहराया क्योंकि उन्होंने यह समझाने में समय लिया कि एक को दूसरे पर क्यों अनुशंसित किया जाता है।
SoCalCoder

40

DriveInfo आपको उनमें से कुछ के साथ मदद करेगा (लेकिन यह UNC पथों के साथ काम नहीं करता है), लेकिन वास्तव में मुझे लगता है कि आपको GetDiskFreeSpaceEx का उपयोग करना होगा । आप शायद WMI के साथ कुछ कार्यक्षमता प्राप्त कर सकते हैं। GetDiskFreeSpaceEx आपके सबसे अच्छे दांव की तरह दिखता है।

संभावना है कि आप इसे ठीक से काम करने के लिए अपने रास्तों को साफ करना होगा।


40

GetDiskFreeSpaceExरिचर्डोड द्वारा लिंक से काम कर कोड स्निपेट ।

// Pinvoke for API function
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetDiskFreeSpaceEx(string lpDirectoryName,
out ulong lpFreeBytesAvailable,
out ulong lpTotalNumberOfBytes,
out ulong lpTotalNumberOfFreeBytes);

public static bool DriveFreeBytes(string folderName, out ulong freespace)
{
    freespace = 0;
    if (string.IsNullOrEmpty(folderName))
    {
        throw new ArgumentNullException("folderName");
    }

    if (!folderName.EndsWith("\\"))
    {
        folderName += '\\';
    }

    ulong free = 0, dummy1 = 0, dummy2 = 0;

    if (GetDiskFreeSpaceEx(folderName, out free, out dummy1, out dummy2))
    {
        freespace = free;
        return true;
    }
    else
    {
        return false;
    }
}

1
मैं बल्कि यह शून्य की तरह होता है ... if (!GetDiskFreeSpaceEx(folderName, out free, out total, out dummy)) throw new Win32Exception(Marshal.GetLastWin32Error());। वैसे भी यहाँ कोड खोजने के लिए काफी सुविधाजनक है।
यूजीन रियात्सेव

2
बस जाँच, लेकिन मुझे लगता है कि "CameraStorageFileHelper" इस कोड से एक कलाकृति है जिसे मूल से कॉपी-पेस्ट किया जा रहा है?
एंड्रयू थेकेन

इसे समाप्त करने की आवश्यकता नहीं है "\\"। यह किसी भी मौजूदा डायर पथ या यहां तक ​​कि बस हो सकता है C:। यहाँ इस कोड का मेरा संस्करण है: stackoverflow.com/a/58005966/964478
एलेक्स पी।

7
using System;
using System.IO;

class Test
{
    public static void Main()
    {
        DriveInfo[] allDrives = DriveInfo.GetDrives();

        foreach (DriveInfo d in allDrives)
        {
            Console.WriteLine("Drive {0}", d.Name);
            Console.WriteLine("  Drive type: {0}", d.DriveType);
            if (d.IsReady == true)
            {
                Console.WriteLine("  Volume label: {0}", d.VolumeLabel);
                Console.WriteLine("  File system: {0}", d.DriveFormat);
                Console.WriteLine(
                    "  Available space to current user:{0, 15} bytes", 
                    d.AvailableFreeSpace);

                Console.WriteLine(
                    "  Total available space:          {0, 15} bytes",
                    d.TotalFreeSpace);

                Console.WriteLine(
                    "  Total size of drive:            {0, 15} bytes ",
                    d.TotalSize);
            }
        }
    }
}
/* 
This code produces output similar to the following:

Drive A:\
  Drive type: Removable
Drive C:\
  Drive type: Fixed
  Volume label: 
  File system: FAT32
  Available space to current user:     4770430976 bytes
  Total available space:               4770430976 bytes
  Total size of drive:                10731683840 bytes 
Drive D:\
  Drive type: Fixed
  Volume label: 
  File system: NTFS
  Available space to current user:    15114977280 bytes
  Total available space:              15114977280 bytes
  Total size of drive:                25958948864 bytes 
Drive E:\
  Drive type: CDRom

The actual output of this code will vary based on machine and the permissions
granted to the user executing it.
*/

1
हालांकि यह कोड वास्तव में एक सिस्टम पर सभी ड्राइव्स के लिए काम करता है, यह माउंट पॉइंट्स और जंक्शन पॉइंट्स और शेयरों के लिए ओपी की आवश्यकता को संबोधित नहीं करता है ...
एड्रियन हम

3

अपरीक्षित:

using System;
using System.Management;

ManagementObject disk = new
ManagementObject("win32_logicaldisk.deviceid="c:"");
disk.Get();
Console.WriteLine("Logical Disk Size = " + disk["Size"] + " bytes");
Console.WriteLine("Logical Disk FreeSpace = " + disk["FreeSpace"] + "
bytes"); 

Btw: सी: \ अस्थायी पर मुक्त डिस्कस्पेस का परिणाम क्या है? आपको c: \


5
जैसा कि केनी कहते हैं, किसी भी निर्देशिका के लिए नि: शुल्क स्थान जरूरी नहीं है कि रूट निर्देशिका के ड्राइव के लिए मुक्त स्थान है। निश्चित रूप से मेरी मशीन पर नहीं है।
बैरी केली

3

यहां @sasha_gud उत्तर का एक परिष्कृत और सरलीकृत संस्करण दिया गया है:

    [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool GetDiskFreeSpaceEx(string lpDirectoryName,
        out ulong lpFreeBytesAvailable,
        out ulong lpTotalNumberOfBytes,
        out ulong lpTotalNumberOfFreeBytes);

    public static ulong GetDiskFreeSpace(string path)
    {
        if (string.IsNullOrEmpty(path))
        {
            throw new ArgumentNullException("path");
        }

        ulong dummy = 0;

        if (!GetDiskFreeSpaceEx(path, out ulong freeSpace, out dummy, out dummy))
        {
            throw new Win32Exception(Marshal.GetLastWin32Error());
        }

        return freeSpace;
    }

आपका रास्ता बेहतर है, आपको लास्टविन 32 एट्रर
एडी श्टेनबर्ग

3

इसे देखें (यह मेरे लिए एक कार्यशील समाधान है)

public long AvailableFreeSpace()
{
    long longAvailableFreeSpace = 0;
    try{
        DriveInfo[] arrayOfDrives = DriveInfo.GetDrives();
        foreach (var d in arrayOfDrives)
        {
            Console.WriteLine("Drive {0}", d.Name);
            Console.WriteLine("  Drive type: {0}", d.DriveType);
            if (d.IsReady == true && d.Name == "/data")
            {
                Console.WriteLine("Volume label: {0}", d.VolumeLabel);
                Console.WriteLine("File system: {0}", d.DriveFormat);
                Console.WriteLine("AvailableFreeSpace for current user:{0, 15} bytes",d.AvailableFreeSpace);
                Console.WriteLine("TotalFreeSpace {0, 15} bytes",d.TotalFreeSpace);
                Console.WriteLine("Total size of drive: {0, 15} bytes \n",d.TotalSize);
                }
                longAvailableFreeSpaceInMB = d.TotalFreeSpace;
        }
    }
    catch(Exception ex){
        ServiceLocator.GetInsightsProvider()?.LogError(ex);
    }
    return longAvailableFreeSpace;
}

2

यह लेख देखें !

  1. ":" का सूचकांक खोज कर UNC बराबर या स्थानीय ड्राइव पथ की पहचान करें

  2. अगर इसका UNC पथ है तो आप UNC पथ को कैम मैप करें

  3. ड्राइव नाम को निष्पादित करने के लिए कोड मैप किया गया ड्राइव नाम है <UNC मैप किया गया ड्राइव या स्थानीय ड्राइव>।

    using System.IO;
    
    private long GetTotalFreeSpace(string driveName)
    {
    foreach (DriveInfo drive in DriveInfo.GetDrives())
    {
        if (drive.IsReady && drive.Name == driveName)
        {
            return drive.TotalFreeSpace;
        }
    }
    return -1;
    }
  4. आवश्यकता होने के बाद अनमैप करें।


1
हालांकि यह कोड वास्तव में एक सिस्टम पर सभी ड्राइव्स के लिए काम करता है, यह माउंट पॉइंट्स और जंक्शन पॉइंट्स के लिए ओपी की आवश्यकता को संबोधित नहीं करता है ...
एड्रियन हम

2

मैं GB में आकार ढूंढ रहा था, इसलिए मैंने निम्नलिखित परिवर्तनों के साथ सुपरमैन के ऊपर दिए गए कोड में सुधार किया:

public double GetTotalHDDSize(string driveName)
{
    foreach (DriveInfo drive in DriveInfo.GetDrives())
    {
        if (drive.IsReady && drive.Name == driveName)
        {
            return drive.TotalSize / (1024 * 1024 * 1024);
        }
    }
    return -1;
}

8
आप ड्राइव की कुल क्षमता लौटा रहे हैं।
निक बिननेट

3
मैंने सोचा था कि कोई भी जीबी बाइट्स की गणना कर सकता है, लेकिन आपने दिखाया कि गलत जमाव था। विभाजन का उपयोग करता है, longलेकिन फ़ंक्शन रिटर्न के रूप में Tis कोड गलत है double
क्वर्टी

2

जैसा कि इस उत्तर और @ रिचर्ड ने सुझाव दिया है, आपको इस तरह करना चाहिए:

[DllImport("kernel32.dll", SetLastError=true, CharSet=CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetDiskFreeSpaceEx(string lpDirectoryName,
   out ulong lpFreeBytesAvailable,
   out ulong lpTotalNumberOfBytes,
   out ulong lpTotalNumberOfFreeBytes);

ulong FreeBytesAvailable;
ulong TotalNumberOfBytes;
ulong TotalNumberOfFreeBytes;

bool success = GetDiskFreeSpaceEx(@"\\mycomputer\myfolder",
                                  out FreeBytesAvailable,
                                  out TotalNumberOfBytes,
                                  out TotalNumberOfFreeBytes);
if(!success)
    throw new System.ComponentModel.Win32Exception();

Console.WriteLine("Free Bytes Available:      {0,15:D}", FreeBytesAvailable);
Console.WriteLine("Total Number Of Bytes:     {0,15:D}", TotalNumberOfBytes);
Console.WriteLine("Total Number Of FreeBytes: {0,15:D}", TotalNumberOfFreeBytes);

1

मैं अपनी परियोजना के लिए एक समान विधि चाहता था लेकिन मेरे मामले में इनपुट रास्ते या तो स्थानीय डिस्क वॉल्यूम या क्लस्टर स्टोरेज वॉल्यूम (CSV) से थे। इसलिए DriveInfo क्लास ने मेरे लिए काम नहीं किया। CSV में एक और ड्राइव के तहत एक माउंट पॉइंट होता है, आमतौर पर C: \ ClusterStorage \ Volume *। ध्यान दें कि C: C: \ ClusterStorage \ Volume1 की तुलना में एक अलग वॉल्यूम होगा

यह वही है जो मैं आखिरकार आया:

    public static ulong GetFreeSpaceOfPathInBytes(string path)
    {
        if ((new Uri(path)).IsUnc)
        {
            throw new NotImplementedException("Cannot find free space for UNC path " + path);
        }

        ulong freeSpace = 0;
        int prevVolumeNameLength = 0;

        foreach (ManagementObject volume in
                new ManagementObjectSearcher("Select * from Win32_Volume").Get())
        {
            if (UInt32.Parse(volume["DriveType"].ToString()) > 1 &&                             // Is Volume monuted on host
                volume["Name"] != null &&                                                       // Volume has a root directory
                path.StartsWith(volume["Name"].ToString(), StringComparison.OrdinalIgnoreCase)  // Required Path is under Volume's root directory 
                )
            {
                // If multiple volumes have their root directory matching the required path,
                // one with most nested (longest) Volume Name is given preference.
                // Case: CSV volumes monuted under other drive volumes.

                int currVolumeNameLength = volume["Name"].ToString().Length;

                if ((prevVolumeNameLength == 0 || currVolumeNameLength > prevVolumeNameLength) &&
                    volume["FreeSpace"] != null
                    )
                {
                    freeSpace = ulong.Parse(volume["FreeSpace"].ToString());
                    prevVolumeNameLength = volume["Name"].ToString().Length;
                }
            }
        }

        if (prevVolumeNameLength > 0)
        {
            return freeSpace;
        }

        throw new Exception("Could not find Volume Information for path " + path);
    }

1

आप यह कोशिश कर सकते हैं:

var driveName = "C:\\";
var freeSpace = DriveInfo.GetDrives().Where(x => x.Name == driveName && x.IsReady).FirstOrDefault().TotalFreeSpace;

सौभाग्य


2
हालांकि यह कोड प्रश्न का उत्तर दे सकता है, लेकिन यह कोड इस और क्यों या कैसे का उत्तर देता है, इस संबंध में अतिरिक्त संदर्भ प्रदान करता है।
xiawi

var ड्राइवनाम = "C: \\";
नीम मेघ

-1

मुझे भी यही समस्या थी और मैंने वारुण मंजुला को सर्वश्रेष्ठ उत्तर देते देखा। हालाँकि यह सब कंसोल पर लिखना यह नहीं है कि आप क्या चाहते हैं। निम्नलिखित जानकारी का उपयोग करने के लिए स्ट्रिंग बंद करने के लिए

एक कदम: शुरू में मूल्यों की घोषणा करें

    //drive 1
    public static string drivename = "";
    public static string drivetype = "";
    public static string drivevolumelabel = "";
    public static string drivefilesystem = "";
    public static string driveuseravailablespace = "";
    public static string driveavailablespace = "";
    public static string drivetotalspace = "";

    //drive 2
    public static string drivename2 = "";
    public static string drivetype2 = "";
    public static string drivevolumelabel2 = "";
    public static string drivefilesystem2 = "";
    public static string driveuseravailablespace2 = "";
    public static string driveavailablespace2 = "";
    public static string drivetotalspace2 = "";

    //drive 3
    public static string drivename3 = "";
    public static string drivetype3 = "";
    public static string drivevolumelabel3 = "";
    public static string drivefilesystem3 = "";
    public static string driveuseravailablespace3 = "";
    public static string driveavailablespace3 = "";
    public static string drivetotalspace3 = "";

चरण 2: वास्तविक कोड

                DriveInfo[] allDrives = DriveInfo.GetDrives();
                int drive = 1;
                foreach (DriveInfo d in allDrives)
                {
                    if (drive == 1)
                    {
                        drivename = String.Format("Drive {0}", d.Name);
                        drivetype = String.Format("Drive type: {0}", d.DriveType);
                        if (d.IsReady == true)
                        {
                            drivevolumelabel = String.Format("Volume label: {0}", d.VolumeLabel);
                            drivefilesystem = String.Format("File system: {0}", d.DriveFormat);
                            driveuseravailablespace = String.Format("Available space to current user:{0, 15} bytes", d.AvailableFreeSpace);
                            driveavailablespace = String.Format("Total available space:{0, 15} bytes", d.TotalFreeSpace);
                            drivetotalspace = String.Format("Total size of drive:{0, 15} bytes ", d.TotalSize);
                        }
                        drive = 2;
                    }
                    else if (drive == 2)
                    {
                        drivename2 = String.Format("Drive {0}", d.Name);
                        drivetype2 = String.Format("Drive type: {0}", d.DriveType);
                        if (d.IsReady == true)
                        {
                            drivevolumelabel2 = String.Format("Volume label: {0}", d.VolumeLabel);
                            drivefilesystem2 = String.Format("File system: {0}", d.DriveFormat);
                            driveuseravailablespace2 = String.Format("Available space to current user:{0, 15} bytes", d.AvailableFreeSpace);
                            driveavailablespace2 = String.Format("Total available space:{0, 15} bytes", d.TotalFreeSpace);
                            drivetotalspace2 = String.Format("Total size of drive:{0, 15} bytes ", d.TotalSize);
                        }
                        drive = 3;
                    }
                    else if (drive == 3)
                    {
                        drivename3 = String.Format("Drive {0}", d.Name);
                        drivetype3 = String.Format("Drive type: {0}", d.DriveType);
                        if (d.IsReady == true)
                        {
                            drivevolumelabel3 = String.Format("Volume label: {0}", d.VolumeLabel);
                            drivefilesystem3 = String.Format("File system: {0}", d.DriveFormat);
                            driveuseravailablespace3 = String.Format("Available space to current user:{0, 15} bytes", d.AvailableFreeSpace);
                            driveavailablespace3 = String.Format("Total available space:{0, 15} bytes", d.TotalFreeSpace);
                            drivetotalspace3 = String.Format("Total size of drive:{0, 15} bytes ", d.TotalSize);
                        }
                        drive = 4;
                    }
                    if (drive == 4)
                    {
                        drive = 1;
                    }
                }

                //part 2: possible debug - displays in output

                //drive 1
                Console.WriteLine(drivename);
                Console.WriteLine(drivetype);
                Console.WriteLine(drivevolumelabel);
                Console.WriteLine(drivefilesystem);
                Console.WriteLine(driveuseravailablespace);
                Console.WriteLine(driveavailablespace);
                Console.WriteLine(drivetotalspace);

                //drive 2
                Console.WriteLine(drivename2);
                Console.WriteLine(drivetype2);
                Console.WriteLine(drivevolumelabel2);
                Console.WriteLine(drivefilesystem2);
                Console.WriteLine(driveuseravailablespace2);
                Console.WriteLine(driveavailablespace2);
                Console.WriteLine(drivetotalspace2);

                //drive 3
                Console.WriteLine(drivename3);
                Console.WriteLine(drivetype3);
                Console.WriteLine(drivevolumelabel3);
                Console.WriteLine(drivefilesystem3);
                Console.WriteLine(driveuseravailablespace3);
                Console.WriteLine(driveavailablespace3);
                Console.WriteLine(drivetotalspace3);

मैं यह नोट करना चाहता हूं कि आप सभी कंसोल रिटनलाइन टिप्पणी कोड बना सकते हैं, लेकिन मुझे लगा कि आपके लिए इसका परीक्षण करना अच्छा होगा। यदि आप एक दूसरे के बाद इन सभी को प्रदर्शित करते हैं तो आपको वारुणा माजूना के समान सूची मिलती है

ड्राइव C: \ ड्राइव प्रकार: फिक्स्ड वॉल्यूम लेबल: फ़ाइल सिस्टम: NTFS वर्तमान उपयोगकर्ता के लिए उपलब्ध स्थान: 134880153600 बाइट्स कुल उपलब्ध स्थान: 134880153600 बाइट्स ड्राइव का कुल आकार: 499554185216 बाइट्स

ड्राइव D: \ ड्राइव प्रकार: CDRom

ड्राइव H: \ ड्राइव प्रकार: फिक्स्ड वॉल्यूम लेबल: HDD फाइल सिस्टम: NTFS वर्तमान उपयोगकर्ता के लिए उपलब्ध स्थान: 2000010817536 बाइट्स कुल उपलब्ध स्थान: 2000010817536 बाइट्स कुल आकार ड्राइव: 2000263573504 बाइट्स

हालाँकि अब आप सभी ढीली सूचनाओं को तार में बदल सकते हैं


7
3 simular ऑब्जेक्ट के लिए एक क्लास नहीं बना रहा है और यदि अन्य का उपयोग कर रहा है। मैं थोड़ा रोया।
मथिजर्स

1
खेद बॉयलर चढ़ाना कोड, संग्रह का उपयोग नहीं कर रहा है और एक स्विच का उपयोग नहीं कर रहा है?
एड्रियन हम
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.