如何检查应用程序是否从C#中的CD/DVD启动?

use*_*884 1 c#

如何检查应用程序是否从C#中的CD/DVD启动?

cod*_*nix 8

使用Application.StartupPath属性获取exe的起始路径.然后使用新的DriveInfo(driveletter_from_path).DriveType来确定它是CD还是硬盘.

  • 你可能想要可执行路径,而不是启动路径(无论程序在哪里,都可以是任何东西). (3认同)

Tho*_*que 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;
        }
Run Code Online (Sandbox Code Playgroud)