C#如何获取指定盘符的设备类型

Jave.Lin 2008-05-28 10:36:16
如标题所问...

如:我要判断...
string str="C:\";
string str1="D:\";
string str2="E:\";
string str3="F:\";
string str4="G:\";
string str5="H:\";
string str6="I:\";
string str7="J:\";
...

就是要获取..str1~str7中的任意一个指定的盘符的设备类型...

设备类型:1.本地硬盘.2.移动硬盘.3.光盘.4.软盘...等等..

本人第一次提问...没什么积分...
...全文
200 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
baihe_591 2008-05-29
  • 打赏
  • 举报
回复
DriveType.CDRom, Fixed Network NoRootDirectory Ram Removable Unknown ;
DriveInfo driveinfo-new DriveInfo("C:\\");
switch(driveinfo.DriveType)
{
case DriveInfo.CDRom:
break;
case DriveInfo.Fixed:
break;
......
}
vrhero 2008-05-28
  • 打赏
  • 举报
回复
[DllImport("kernel32.dll", EntryPoint="GetDriveTypeA")]
public static extern int GetDriveType (string nDrive);
Jave.Lin 2008-05-28
  • 打赏
  • 举报
回复
大哥能给出
C#下的GetDriveTypeA声明格式吗?

你那个是VB的,我对VB不了解...
如:
我调用Hook的
[DllImport("user32.dll")]
public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);
[DllImport("user32.dll")]
public static extern bool UnhookWindowsHookEx(int idHook);
[DllImport("user32.dll")]
public static extern int CallNextHookEx(int idHook, int nCode, Int32 wParam, IntPtr lParam);

那你现在这个GetDriveTypeA是这样?

[DllImport("kernel32.dll")]
public static extern int GetDriveTypeA(string nDrive);

是这样吗?

我在MSDN里找不到...,找到的就只有VB的...而且它连声明格式也没有列出来...就直接写到VB的执行代码...
vrhero 2008-05-28
  • 打赏
  • 举报
回复
2003要调用Win32 API...

Public Declare Function GetDriveType Lib "kernel32" Alias "GetDriveTypeA" (ByVal nDrive As String) As Integer
Jave.Lin 2008-05-28
  • 打赏
  • 举报
回复
谢谢1楼的朋友...

我是编程菜鸟...很多东西不懂...MSDN比较少用到...

我还有一个问题...我现在用的是VS 2003的...那个System.IO.DriveInfo类,在2005或2005以上的版才有的

相对2003是个新增的东西...那现在能在2003...而又没用到System.IO.DriveInfo的情况下...能做到不?

我猜应该可以的,只是我们不知道而已,不然2003之前的那些人是怎么做到的?
vrhero 2008-05-28
  • 打赏
  • 举报
回复
System.IO.DriveInfo.DriveType 属性...

DriveType 属性指示驱动器是否是以下任意类型:CDRom、Fixed、Unknown、Network、NoRootDirectory、Ram、Removable 或 Unknown。值在 DriveType 枚举中列出。

这种问题你去查MSDN...1分钟就搞定...还省20分...

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧