Reputation: 65
How Can I Check To See If App was Started From A CD/DVD in C#?
Upvotes: 2
Views: 280
Reputation: 60902
Expanding on codemanix's answer:
string location = Assembly.GetExecutingAssembly().Location;
DriveInfo info = new DriveInfo(Path.GetPathRoot(location));
if (info.DriveType == DriveType.CDRom)
{
Console.WriteLine("Started from CD-ROM");
}
MSDN: description of the drive types.
Upvotes: 4
Reputation: 52123
I'm not completely sure why are you doing it but, just in case it is an attempt of copy protection remember the old (ancient) subst in MS-DOS.
Just keep in mind that using Application.ExecutablePath and DriveInfo can be forged...
Upvotes: 1
Reputation: 292425
You can do something like that :
FileInfo file = new FileInfo(Process.GetCurrentProcess().MainModule.FileName);
DriveInfo drive = new DriveInfo(file.Directory.Root.ToString());
switch (drive.DriveType)
{
case DriveType.CDRom:
MessageBox.Show("Started from CD/DVD");
break;
case DriveType.Network:
MessageBox.Show("Started from network");
break;
case DriveType.Removable:
MessageBox.Show("Started from removable drive");
break;
default:
break;
}
Upvotes: 7
Reputation: 29468
Get the path where the exe was start from with Application.StartupPath property. then use new DriveInfo(driveletter_from_path).DriveType to determine whether it is a CD or harddisk.
Upvotes: 8
Reputation: 26998
You need to check the executable path and see if it is on the CD/DVD drive. You can get the executable path with this:
string path = Application.ExecutablePath;
Upvotes: 3