[英]How Can I Check To See If App was Started From A CD/DVD in C#?
如何检查是否从C#中的CD / DVD启动了应用程序?
Get the path where the exe was start from with Application.StartupPath property. 使用Application.StartupPath属性获取exe所在的路径。 then use new DriveInfo(driveletter_from_path).DriveType to determine whether it is a CD or harddisk.
然后使用新的DriveInfo(driveletter_from_path).DriveType确定它是CD还是硬盘。
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: 扩展codemanix的答案:
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. 您需要检查可执行路径,然后查看它是否在CD / DVD驱动器上。 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. 我不能完全肯定你为什么这样做,但以防万一它是复制保护的企图老记(古) SUBST在MS-DOS。
Just keep in mind that using Application.ExecutablePath and DriveInfo can be forged... 请记住,可以伪造使用Application.ExecutablePath和DriveInfo ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.