using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using Microsoft.Win32;
namespace UpData
{
public partial class FindUsb
{
[DllImport("kernel32")]
public FindUsb()
{
String[] logicDrivers = Environment.GetLogicalDrives(); ///返回驱动器名字
Console.WriteLine(String.Join(", ", logicDrivers));
int i=0;
for(;i< logicDrivers.Length;i++)
{
switch (GetDriveType(logicDrivers[i]))
{
case 3:
Console.WriteLine("root");
continue ;
case 5:
Console.WriteLine("CD");
continue;
case 2:
Console.WriteLine("USB");
continue;
default:
Console.WriteLine("unknown");
continue;
}
}
}
}
}
代码如上,现在VS报错:
错误 1 属性“DllImport”在该声明类型中无效。它只在“method”声明中有效。
错误 2 当前上下文中不存在名称“GetDriveType”
请教,这是什么问题?该如何解决?
C#代码 判断磁盘是不是U盘
答案:4 悬赏:40 手机版
解决时间 2021-03-19 11:02
- 提问者网友:战皆罪
- 2021-03-18 18:36
最佳答案
- 五星知识达人网友:封刀令
- 2021-03-18 20:12
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace WindowsApplication3
{
public class FindUSB
{
public static List<String> FindUSBDriver()
{
List<String> driverList = new List<string>();
DriveInfo[] myAllDrives = DriveInfo.GetDrives();
foreach (DriveInfo myDrive in myAllDrives) {
if (myDrive.IsReady) {
if (myDrive.DriveType == DriveType.Removable) {
driverList.Add(myDrive.Name);
}
}
}
return driverList;
}
}
}
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace WindowsApplication3
{
public class FindUSB
{
public static List<String> FindUSBDriver()
{
List<String> driverList = new List<string>();
DriveInfo[] myAllDrives = DriveInfo.GetDrives();
foreach (DriveInfo myDrive in myAllDrives) {
if (myDrive.IsReady) {
if (myDrive.DriveType == DriveType.Removable) {
driverList.Add(myDrive.Name);
}
}
}
return driverList;
}
}
}
全部回答
- 1楼网友:过活
- 2021-03-18 23:38
using System.IO;
DriveInfo[] drive = DriveInfo.GetDrives();
for (int i = 0; i < drive.Length;i++ )
{
if (drive[i].IsReady)
{
drive[i].Name //名称
drive[i].TotalSize //大小
drive[i].TotalFreeSpace //剩余空间
}
}
- 2楼网友:醉吻情书
- 2021-03-18 22:20
[DllImport("kernel32.dll")]
再看看别人怎么说的。
- 3楼网友:思契十里
- 2021-03-18 21:17
要声明你引用了哪个DLL好像。。。
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯