Tutorial :How Can I Check To See If App was Started From A CD/DVD in C#?


How Can I Check To See If App was Started From A CD/DVD in C#?


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.


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


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.


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;  


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

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »