如何查看是否从C#中的CD / DVD启动应用程序?
答案 0 :(得分:8)
使用Application.StartupPath属性获取exe的起始路径。 然后使用新的DriveInfo(driveletter_from_path).DriveType来确定它是CD还是硬盘。
答案 1 :(得分:8)
你可以这样做:
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;
}
答案 2 :(得分:4)
扩展codemanix的答案:
string location = Assembly.GetExecutingAssembly().Location;
DriveInfo info = new DriveInfo(Path.GetPathRoot(location));
if (info.DriveType == DriveType.CDRom)
{
Console.WriteLine("Started from CD-ROM");
}
答案 3 :(得分:3)
您需要检查可执行文件路径,看它是否在CD / DVD驱动器上。您可以使用以下命令获取可执行文件路径:
string path = Application.ExecutablePath;
答案 4 :(得分:1)
我不完全确定你为什么要这样做,但是,如果它是一个复制保护的尝试,请记住MS-DOS中的旧(古代)subst。
请记住,使用Application.ExecutablePath和DriveInfo可以伪造......