user112884
user112884

Reputation: 65

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#?

Upvotes: 2

Views: 280

Answers (5)

Michael Petrotta
Michael Petrotta

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

Jorge Córdoba
Jorge Córdoba

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

Thomas Levesque
Thomas Levesque

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

codymanix
codymanix

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

Zifre
Zifre

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

Related Questions