2009-06-06 41 views

回答

8

获取的路径,其中的EXE是从Application.StartupPath财产开始。 然后使用新的DriveInfo(driveletter_from_path).DriveType来确定它是CD还是硬盘。

+3

您可能需要可执行文件路径,而不是启动路径(可能是任何东西,无论程序在哪里)。 – Zifre 2009-06-06 23:41:04

3

您需要检查可执行文件路径,看它是否在CD/DVD驱动器上。你可以利用这个得到可执行文件的路径:

string path = Application.ExecutablePath; 
+1

如何知道路径是否位于CD/DVD驱动器上? – 2009-06-06 23:42:30

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

MSDN: description of the drive types.

1

我不能完全肯定你为什么这样做,但,就在情况下它是一个复制保护的尝试记住MS-DOS中的旧(古代)subst

请记住,使用Application.ExecutablePath和DriveInfo可以伪造...

相关问题