You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

驱动器信息转字符串数组报错,求指导C#磁盘检测重写代码问题

Hey there! Let's work through your issues step by step—first addressing that old conversion error, then fixing your current drive detection code since it's got some logic snags right now.

1. Fixing the "Error converting drive info to a string array" Issue

That error pops up when you try to directly assign a DriveInfo[] array (the result of DriveInfo.GetDrives()) to a string[] variable. They're different types—DriveInfo is an object with properties like Name, DriveType, etc., not a raw string.

To fix it, either:

  • Keep it as a DriveInfo[] if you need access to all drive properties:
    DriveInfo[] drives = DriveInfo.GetDrives();
    
  • Or convert it to a string array if you only need drive names/paths:
    using System.Linq; // Don't forget this using directive at the top of your file
    
    string[] drivePaths = DriveInfo.GetDrives()
                                   .Select(drive => drive.Name) // Gets the root path like "C:\"
                                   .ToArray();
    

2. Fixing Your Current Drive Detection & Confirmation Code

Your current code has a few logic issues: it loops through all existing drives first, prompts for USB insertion after already listing drives, and the confirmation flow is reversed (pressing Enter was triggering an exit instead of proceeding). Here's a cleaned-up, functional version with explanations:

using System.IO;

public static void ProcessUsbDrive()
{
    // First, wait for the user to insert their USB
    Console.WriteLine("Please insert your USB drive, then press Enter when ready...");
    Console.ReadLine(); // Pauses until user presses Enter

    // Get all drives AFTER the user has inserted the USB
    DriveInfo[] allDrives = DriveInfo.GetDrives();

    foreach (DriveInfo drive in allDrives)
    {
        // Skip drives that aren't ready or aren't removable (like internal hard drives)
        if (!drive.IsReady || drive.DriveType != DriveType.Removable)
            continue;

        // Show clear drive info instead of raw DriveInfo object
        Console.WriteLine($"\nDetected removable drive: {drive.Name}");
        if (!string.IsNullOrEmpty(drive.VolumeLabel))
            Console.WriteLine($"Drive label: {drive.VolumeLabel}");

        // First confirmation: proceed with this drive?
        Console.WriteLine("Proceed with this drive? (Press Enter to continue, any other key to skip.)");
        if (Console.ReadKey().Key == ConsoleKey.Enter)
        {
            // Second confirmation for data erasure (critical warning!)
            Console.WriteLine("\n⚠️ WARNING: This will erase ALL data on the drive! ⚠️");
            Console.WriteLine("Are you ABSOLUTELY sure? (Press Enter to confirm, any other key to cancel.)");
            
            if (Console.ReadKey().Key == ConsoleKey.Enter)
            {
                // Add your drive erasure/processing logic here
                Console.WriteLine($"\nStarting processing on drive {drive.Name}...");
                break; // Exit loop once we've selected the target drive
            }
            else
            {
                Console.WriteLine("\nOperation cancelled.");
                return; // Exit the method entirely
            }
        }
        else
        {
            Console.WriteLine("\nSkipping this drive.");
        }
    }

    Console.WriteLine("\nNo valid removable drive was detected or selected.");
}

Key fixes and improvements:

  • Waits for USB insertion first: The code pauses until the user inserts their USB and presses Enter, then scans for drives—this fixes the backwards flow in your original code.
  • Filters removable drives: Skips internal hard drives, CD/DVD drives, etc., so you only see USBs.
  • Clear confirmation flow: Pressing Enter now means "proceed" (not exit), which aligns with user expectations.
  • Readable drive info: Uses drive.Name and drive.VolumeLabel instead of printing the raw DriveInfo object (which would just show System.IO.DriveInfo otherwise).
  • Safety checks: Adds a clear warning for data erasure and a second confirmation to prevent accidental data loss.

内容的提问来源于stack exchange,提问作者nxfi777

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 22:37:42