Absinthe
Absinthe

Reputation: 3391

List all disks except DVD drive without using DriveInfo.GetDrives

How can I list all disks except DVD drives without using DriveInfo.GetDrives? I'm using the Unity game engine and it throws an error on GetDrives:

NotImplementedException: The requested feature is not implemented. System.IO.DriveInfo.WindowsGetDrives

I'm looking for a workaround. I've read that Winbase.h has a DriveType enum, is there any way I could use that?

Upvotes: 0

Views: 939

Answers (2)

Absinthe
Absinthe

Reputation: 3391

Thanks to Amy's answer I found this post by C.R. Timmons Consulting, Inc. Tested and working on Windows, I'd be grateful if someone on Mac could check if it works too:

public enum DriveType : int
{
Unknown = 0,
NoRoot = 1,
Removable = 2,
Localdisk = 3,
Network = 4,
CD = 5,
RAMDrive = 6
}

[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern int GetDriveType(string lpRootPathName);

Example usage:

using System;
using System.Runtime.InteropServices;

void GetDrives()
{
     foreach (string s in Environment.GetLogicalDrives())
     Console.WriteLine(string.Format("Drive {0} is a {1}.",
     s, Enum.GetName(typeof(DriveType), GetDriveType(s))));
}

Upvotes: 0

user47589
user47589

Reputation:

You can't, not without sacrificing platform-independence.

The following will get all drives. I don't think there's a platform-independent way of getting "all drives except DVD":

string[] drives = Directory.GetLogicalDrives();

You mentioned Winbase.h. That's C++, and will tie you to a particular platform (Windows). You can do this, using p/invoke, but you will have to write platform-dependent code. I wouldn't recommend P/invoke for beginners. It's an advanced topic that can get difficult pretty quickly.

What does "platform-dependent" mean? You would have to write code for Windows, code for Linux, code for Mac, and any other platforms your code will run on. One of Unity's selling points is you can write code once and expect it to run the same on a variety of different platforms.

Upvotes: 1

Related Questions