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;
}
}
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.