Frage

I'm trying to detect if our application is running from a DVD (because this disables/enables functionality in the logic). So far I've come up with the code snippet below that seems to work, though I was really wondering if there's a best-practice in detecting this.

    public static bool IsDVDInstallation()
    {
        try
        {
            string location = Assembly.GetExecutingAssembly().Location;
            var info = new DriveInfo(Path.GetPathRoot(location));
            return info.DriveType == DriveType.CDRom;
        }
        catch
        {
            return false;
        }
    }
War es hilfreich?

Lösung

If you want to know if the application (rather than whatever particular assembly you're in) is running on an optical drive, then you probably should use GetEntryAssembly() rather than GetExecutingAssembly(). Other than that, your logic above seems perfectly reasonable.

Why the silent catch block? Did you get an exception when trying this before? Even if you did, you really should capture the specific exceptions that you know how to handle rather than everything.

Andere Tipps

Your solution is about as solid as it gets. However it will still function if running from a Virtual DVD Drive (which I assume you don't want)

In which case you'd have to query WMI to get more information on the hardware to try and figure out if it is 'real' or not - but this is no safe bet either, so your existing solution should cover you for anything except power users who know what they're doing (who you wouldn't be able to do much against anyway)

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top