简体   繁体   English

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

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

MSDN: description of the drive types. MSDN:驱动器类型的描述。

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.

相关问题 我可以在C#中获得CD / DVD上的复制保护类型吗? - Can I get the type of copy protection on a CD/DVD in C#? 从DVD和CD C#复制和读取文件 - Copy and read files from dvd and CD C# 如何判断光驱(不是光盘)是CD还是DVD驱动器? - How can I tell if an optical drive (not the disc) is a CD or DVD drive? 我如何调试在外部C#应用程序中切换的C ++ DLL(无法从调试器启动) - How can i debug C++ DLLs SWIGed in external C# app (that can not be started from debugger) 在 Windows 10 上,如何使用 shell 执行动词“SendTo”以编程方式从 C# 将一组文件发送到 CD/DVD 刻录机 - On Windows 10, how to send programmatically from C# a set of files to the CD/DVD recorder using the shell execution verb "SendTo" 如何发现USB存储设备和可写CD / DVD驱动器(C#) - How to discover USB storage devices and writable CD/DVD drives (C#) 如何在C#应用程序中没有IMAPI的情况下检测CD / DVD驱动器中的媒体类型? - How to detect media type in a CD/DVD drive without IMAPI in a C# application? 是否有一种相对简单的方法来在 C# 或 PowerShell 中完成 CD 或 DVD? - Is there a relatively straightforward way to finalize a CD or DVD in C# or PowerShell? 尝试读取 dvd/cd 驱动器并在 C# 中填充组合框 - trying to read dvd/cd drives and populate combo box in c# C# - 如何重命名我启动的进程窗口? - C# - How can I rename a process window that I started?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM