2009-06-06 8 views

Répondre

8

Obtenez le chemin où l'exe était de commencer avec Application.StartupPath propriété. puis utilisez le nouveau DriveInfo (driveletter_from_path) .DriveType pour déterminer s'il s'agit d'un CD ou d'un disque dur.

+3

Vous voudriez probablement le chemin exécutable, pas le chemin de démarrage (qui pourrait être n'importe quoi, peu importe où le programme est). – Zifre

+1

Nice info sur la classe DriveInfo. http://msdn.microsoft.com/fr-fr/library/system.io.driveinfo.aspx –

3

Vous devez vérifier le chemin de l'exécutable et vérifier s'il se trouve sur le lecteur de CD/DVD. Vous pouvez obtenir le chemin exécutable avec ceci:

string path = Application.ExecutablePath; 
+1

Et comment saurez-vous si un chemin est sur un lecteur de CD/DVD? –

8

Vous pouvez faire quelque chose comme ça:

 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

L'expansion sur la réponse 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

Je ne suis pas complètement sûr pourquoi faites-vous, mais, juste S'il s'agit d'une tentative de protection de la copie, rappelez-vous l'ancien (ancien) subst dans MS-DOS.

Il suffit de garder à l'esprit que l'utilisation Application.ExecutablePath et DriveInfo ... peuvent être forgées

Questions connexes