42

This may seem like a stupid question, so here goes:

Other than parsing the string of FileInfo.FullPath for the drive letter to then use DriveInfo("c") etc to see if there is enough space to write this file. Is there a way to get the drive letter from FileInfo?

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
maxfridbe
  • 3,221
  • 3
  • 23
  • 13
  • Warning: This will not work in all cases! Just because there is enough space on the root of a drive doesn't mean there is enough space in the current directory. Likewise there might not be the space at the root but there is space in the current directory. Windows at least is capable of figuring out the space in the current directory as evidenced by multiple programs correctly reporting the free space in the current directory even when it doesn't match the space at the root. I have not investigated how to accomplish this. (The situation I'm thinking of is volumes mapped into subdirectories.) – Loren Pechtel Jun 18 '10 at 02:30

4 Answers4

77
FileInfo f = new FileInfo(path);    
string drive = Path.GetPathRoot(f.FullName);

This will return "C:\". That's really the only other way.

BFree
  • 102,548
  • 21
  • 159
  • 201
29

Well, there's also this:

FileInfo file = new FileInfo(path);
DriveInfo drive = new DriveInfo(file.Directory.Root.FullName);

And hey, why not an extension method?

public static DriveInfo GetDriveInfo(this FileInfo file)
{
    return new DriveInfo(file.Directory.Root.FullName);
}

Then you could just do:

DriveInfo drive = new FileInfo(path).GetDriveInfo();
Dan Tao
  • 125,917
  • 54
  • 300
  • 447
  • GetDriveInfo doesn't seem to exist on FileInfo, I only found a reference to it in VB, not C#. – Josh Apr 27 '18 at 15:57
  • 1
    `DriveInfo` calls `GetPathRoot()` so this is extra overhead if all one wants is the drive letter. On the other hand, cool; I didn't know `DriveInfo` existed until now. – Still.Tony Sep 09 '18 at 16:28
-3

You can get all drive in system using this code :

foreach (DriveInfo objDrive in DriveInfo.GetDrives())
    {
        Response.Write("</br>Drive Type : " + objDrive.Name);
        Response.Write("</br>Drive Type : " + objDrive.DriveType.ToString());
        Response.Write("</br>Available Free Space : " + objDrive.AvailableFreeSpace.ToString() + "(bytes)");
        Response.Write("</br>Drive Format : " + objDrive.DriveFormat);
        Response.Write("</br>Total Free Space : " + objDrive.TotalFreeSpace.ToString() + "(bytes)");
        Response.Write("</br>Total Size : " + objDrive.TotalSize.ToString() + "(bytes)");
        Response.Write("</br>Volume Label : " + objDrive.VolumeLabel);
        Response.Write("</br></br>");

    }
Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
Jayesh Sorathia
  • 1,596
  • 15
  • 16
-3

Nothing wrong with a little string parsing :-)

FullPath.Substring(0,1);
Osama AbuSitta
  • 3,918
  • 4
  • 35
  • 51
Joel Martinez
  • 46,929
  • 26
  • 130
  • 185
  • 23
    This is making unsafe assumptions about the path. Consider the case where it is actually a UNC pathname of the form \\machinename\share\path\filename.txt. – Steven Sudit Sep 18 '09 at 13:39
  • 12
    When it comes to filesystem paths, everything is `wrong with a little string parsing :-)` – turbo Jan 07 '19 at 10:01