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
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
> 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
Yes. RTFM.