如何检查是否从C#中的CD / DVD启动了应用程序?
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");
}
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...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.