C
C#2y ago
Surihia

Is there a way to determine the free space on a drive by using a text file ?

I have a path to a folder which is stored in a text file: D:\Projects\Videos\current\ I want the program to take the drive letter from the text file and then show the space on the console. Currently my code is as follows which only shows the free space on my root drive C.
static void Main(string[] args)
{
var list = new List<string>();
var DrivepathTxtFile = new FileStream("LocatedPath.txt", FileMode.Open, FileAccess.Read);


StreamReader reader = new StreamReader(DrivepathTxtFile);

string DrivePath;
while ((DrivePath = reader.ReadLine()) != null)
{
list.Add(DrivePath);
}

DriveInfo[] Drives = DriveInfo.GetDrives();

foreach (DriveInfo d in Drives)
{
Console.WriteLine(d.TotalFreeSpace);
Console.ReadLine();
}
static void Main(string[] args)
{
var list = new List<string>();
var DrivepathTxtFile = new FileStream("LocatedPath.txt", FileMode.Open, FileAccess.Read);


StreamReader reader = new StreamReader(DrivepathTxtFile);

string DrivePath;
while ((DrivePath = reader.ReadLine()) != null)
{
list.Add(DrivePath);
}

DriveInfo[] Drives = DriveInfo.GetDrives();

foreach (DriveInfo d in Drives)
{
Console.WriteLine(d.TotalFreeSpace);
Console.ReadLine();
}
I copied the code straight from the MS page and this obviously does not work as the code is picking my C Drive's space.
14 Replies
Up
Up2y ago
Path.GetPathRoot() compare that to the Name property of the drive info
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Surihia
Surihia2y ago
yes. I am having issue there too. ok a bit confused. I added the pathRoot to see the DrivePath string. I do not understand the comparing with the Name property part tho. string pathRoot; pathRoot = Path.GetPathRoot(DrivePath);
ross
ross2y ago
Does it specifically have to be done via a text file and a certain drive? Any reason you aren’t just getting the capacity of all available drives?
Surihia
Surihia2y ago
Yes I want it to be done specifically by text files.
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Up
Up2y ago
gross..
Surihia
Surihia2y ago
? can you tell me what your code is doing ?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Surihia
Surihia2y ago
got an exception
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Surihia
Surihia2y ago
that works thank you so much
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Surihia
Surihia2y ago
I was trying to figure out how exactly this worked. I still consider myself new to C# 😅