Get free disk space

前端 未结 13 1583
孤街浪徒
孤街浪徒 2020-12-02 13:04

Given each of the inputs below, I\'d like to get free space on that location. Something like

long GetFreeSpace(string path)

Inputs:

13条回答
  •  眼角桃花
    2020-12-02 13:14

    I had the same problem and i saw waruna manjula giving the best answer. However writing it all down on the console is not what you might want. To get string off al info use the following

    Step one: declare values at begin

        //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 = "";
    

    Step 2: actual code

                    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);
    

    I want to note that you can just make all the console writelines comment code, but i thought it would be nice for you to test it. If you display all these after each other you get the same list as waruna majuna

    Drive C:\ Drive type: Fixed Volume label: File system: NTFS Available space to current user: 134880153600 bytes Total available space: 134880153600 bytes Total size of drive: 499554185216 bytes

    Drive D:\ Drive type: CDRom

    Drive H:\ Drive type: Fixed Volume label: HDD File system: NTFS Available space to current user: 2000010817536 bytes Total available space: 2000010817536 bytes Total size of drive: 2000263573504 bytes

    However you can now acces all of the loose information at strings

提交回复
热议问题