Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

GetDrives() in C++

557 views
Skip to first unread message

Franz Bachler

unread,
Jul 4, 2009, 4:44:26 AM7/4/09
to
Hello,

i found and adapted a program for showing a drive list in C# and would like
to translate it in C++:

using System;
using System.Collections.Generic;
using System.Text;

namespace Diskinfo
{
class Program
{
static void Main(string[] args)
{
System.IO.DriveInfo[] allDrives =
System.IO.DriveInfo.GetDrives();
StringBuilder ls = new StringBuilder();
int s;
string str;
foreach (System.IO.DriveInfo d in allDrives)
{
// not access to disk drive A:
s = String.Compare(d.Name,"A:\\");
if (s != 0)
{
ls.Append(Environment.NewLine);
ls.Append(d.Name);
if (d.IsReady == true)
{
str = d.VolumeLabel;
if (str.Length < 8) str += " ";
if (str.Length < 12) str += " ";
ls.Append(String.Format("{0} \t", str));
ls.Append(String.Format("{0} \t", d.DriveFormat));
str = d.DriveType.ToString();
if (str.Length < 8) str += " ";
ls.Append(String.Format("{0} \t", str));
ls.Append(String.Format("{0} \t",
(d.AvailableFreeSpace / 1024 / 1024).ToString()));
ls.Append(String.Format("{0} \t", (d.TotalSize /
1024 / 1024).ToString()));
}
// ls.Append(Environment.NewLine);
}
}
Console.WriteLine(ls.ToString());
}
}
}

I would need an example code in C++.

Greetings,
Franz
--
Franz Bachler, A-3250 Wieselburg
E-Mail: fraba (at) gmx.at
Homepage: http://members.aon.at/fraba
oder http://home.pages.at/fraba


Alf P. Steinbach

unread,
Jul 4, 2009, 5:31:39 AM7/4/09
to
* Franz Bachler:

>
> i found and adapted a program for showing a drive list in C# and would like
> to translate it in C++:
>
> using System;
> using System.Collections.Generic;
> using System.Text;
>
> namespace Diskinfo
> {
> class Program
> {
> static void Main(string[] args)
> {
> System.IO.DriveInfo[] allDrives =
> System.IO.DriveInfo.GetDrives();
> StringBuilder ls = new StringBuilder();
> int s;
> string str;
> foreach (System.IO.DriveInfo d in allDrives)
> {
> // not access to disk drive A:
> s = String.Compare(d.Name,"A:\\");

Is it really the case that DriveInfo willy-nilly doesn't support drive A?


> if (s != 0)
> {
> ls.Append(Environment.NewLine);
> ls.Append(d.Name);
> if (d.IsReady == true)
> {
> str = d.VolumeLabel;
> if (str.Length < 8) str += " ";
> if (str.Length < 12) str += " ";
> ls.Append(String.Format("{0} \t", str));
> ls.Append(String.Format("{0} \t", d.DriveFormat));
> str = d.DriveType.ToString();
> if (str.Length < 8) str += " ";
> ls.Append(String.Format("{0} \t", str));
> ls.Append(String.Format("{0} \t",
> (d.AvailableFreeSpace / 1024 / 1024).ToString()));
> ls.Append(String.Format("{0} \t", (d.TotalSize /
> 1024 / 1024).ToString()));
> }
> // ls.Append(Environment.NewLine);
> }
> }
> Console.WriteLine(ls.ToString());
> }
> }
> }
>
> I would need an example code in C++.

Not C++, but...

C:\> wmic logicaldisk list brief

That starts a silly background process that one would rather be without, but it
can be killed. :-)

If you just want a list of drive letters (must be run with administrator rights),

C:\> fsutil fsinfo drives

For creating your own drivelist program, one that doesn't require administrator
rights, the easiest is to use VBScript or JScript; I believe the filsystem
object gives access to this information.

As a starting point for C++ at the API level (if that's what you want),

<code>
#include <iostream>
#include <bitset>
#include <string>
#include <windows.h>

int main()
{
using namespace std;

bitset<32> const driveExists = GetLogicalDrives();
for( int i = 0; i < 32; ++i )
{
if( driveExists[i] )
{
char const driveLetter = char( 'A' + i );
string const rootPath = string() + driveLetter + ":\\";
UINT const type = GetDriveType( rootPath.c_str() );

cout << "Drive " << driveLetter << " is type " << type << endl;
}
}
cout << endl;
}
</code>


Cheers & hth.,

- Alf

Franz Bachler

unread,
Jul 5, 2009, 1:31:05 AM7/5/09
to
Hello,

> Is it really the case that DriveInfo willy-nilly doesn't support drive A?

But the floppy drive always clatter without a disk inside and I don't want
to damage it ...

> As a starting point for C++ at the API level (if that's what you want),

> <code>
> #include <iostream>
> #include <bitset>
> #include <string>
> #include <windows.h>
>
> int main()
> {
> using namespace std;
>
> bitset<32> const driveExists = GetLogicalDrives();
> for( int i = 0; i < 32; ++i )
> {
> if( driveExists[i] )
> {
> char const driveLetter = char( 'A' + i );
> string const rootPath = string() + driveLetter + ":\\";
> UINT const type = GetDriveType(
> rootPath.c_str() );
>
> cout << "Drive " << driveLetter << " is type " << type <<
> endl;
> }
> }
> cout << endl;
> }
> </code>

Here you see only the DriveType but is there a way to add the drive size and
the free space?

Greetings
Franz


Alf P. Steinbach

unread,
Jul 5, 2009, 10:14:12 AM7/5/09
to
* Franz Bachler:

Yes. RTFM.

0 new messages