vb.net硬盘 vbnet msdn

vb.net 磁盘文件列表 , 界面如图 , 在.net下如何实现?预先准备三个图标文件,用于树型控件中显示磁盘符号和文件夹的图像之用 。
1、窗体上添加控件如下:
组合框控件 ComboBox1,树型控件 TreeView1,列表框控件 ListBox1,图像列表控件 ImageList1 。
选中TreeView1,设置其ImageList属性为ImageList1 。
2、设置属性
选中图像列表控件 ImageList1,在属性窗口里 , 选中属性Images,单击三个小点按钮 , 出现图像集合编辑器窗口 , 单击[添加按钮],一一把准备好的图标文件进行添加,注意先后次序,如果不符合要求可以通过上下移动按钮重新改变次序 。完成后单击[确定] 。
运行图如下:
完整代码如下:
Imports System.IO
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'添加系统所有磁盘目录符号
For Each MyDrive As String In Environment.GetLogicalDrives()
ComboBox1.Items.Add(MyDrive)
Next
'显示第一个磁盘符号
ComboBox1.Text = ComboBox1.Items(0)
End Sub
'递归过程添加目录树
Public Sub AddDirectory(ByVal strFatherPath As String, ByVal strPath As String, ByVal nodeFather As TreeNode)
Dim i As Integer
Dim Mynode As New TreeNode
'先添加本目录
Mynode.Text = Strings.Replace(strPath, strFatherPath"\", "", , 1)
'为节点指定未被选中时显示的图标
Mynode.ImageIndex = 1
'为节点指定被选中时显示的图标
Mynode.SelectedImageIndex = 2
nodeFather.Nodes.Add(Mynode)
Application.DoEvents()
Try
Dim str() As String = Directory.GetDirectories(strPath)
'递归遍历该目录的子文件夹
For i = 0 To str.GetUpperBound(0)
AddDirectory(strPath, str(i), Mynode)
Next
Catch ex As Exception
Debug.WriteLine(ex.Message)
End Try
Mynode = Nothing
End Sub
'根据给出的盘符添加目录树
Private Sub AddRootDirectory(ByVal DiscSymbol As String)
Dim Nynode As New TreeNode
'先把磁盘盘符添加到树中
TreeView1.Nodes.Clear()
Nynode.ImageIndex = 0
Nynode.Text = DiscSymbol
Nynode.SelectedImageIndex = -1
TreeView1.Nodes.Add(Nynode)
Dim i As Integer
'获取磁盘根目录下的文件夹
Dim str() As String = Directory.GetDirectories(DiscSymbol"\")
For i = 0 To str.GetUpperBound(0)
'调用递归过程遍历该文件夹里的所有子文件夹 , 并添加到树型控件
AddDirectory(DiscSymbol, str(i), Nynode)
Next
Nynode = Nothing
End Sub
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
'根据磁盘符号的变更 , 显示根目录里的文件
ListBox1.Items.Clear()
For Each MyFile As String In System.IO.Directory.GetFiles(ComboBox1.Text)
ListBox1.Items.Add(MyFile)
Next
'根据磁盘符号的变更,重新显示目录树
Dim DiscSymbol As String
DiscSymbol = Microsoft.VisualBasic.Left(ComboBox1.Text, Len(ComboBox1.Text) - 1)
Call AddRootDirectory(DiscSymbol)
End Sub
'递归过程根据子目录寻找上级目录名--从而构成完整的目录路径
Private Sub AllPath(ByVal ThisNode As TreeNode, ByRef MyPathName As String)
If ThisNode.Level1 Then
'该节点层数大于1,其父节点不是磁盘根目录
MyPathName = ThisNode.Parent.Text"\"MyPathName
Dim MyNode As TreeNode = ThisNode.Parent
Call AllPath(MyNode, MyPathName)
Else
'该节点层数等于1,其父节点就是磁盘根目录
MyPathName = ComboBox1.TextMyPathName
End If
End Sub
Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
'为了搜索选中的节点对应目录的文件 , 需要组成全路径
Dim MyAllPathName As String = TreeView1.SelectedNode.Text
Dim MyNode As TreeNode = TreeView1.SelectedNode
If TreeView1.SelectedNode.Level = 0 Then
'如果选中的是根节点
MyAllPathName = ComboBox1.Text
Else
'如果选中的是非根节点,调用递归过程组成全路径
Call AllPath(MyNode, MyAllPathName)
MyAllPathName = MyAllPathName"\"
End If
'根据路径,搜索文件名并显示
ListBox1.Items.Clear()
For Each MyFile As String In System.IO.Directory.GetFiles(MyAllPathName)
ListBox1.Items.Add(MyFile)
Next
End Sub
End Class
vb.net 判断当前目录是否为移动硬盘Set fso = CreateObject("scripting.filesystemobject")Set disks = fso.DrivesFor Each disk In disks '枚举磁盘Set ID = fso.GetDrive(fso.GetDriveName(disk))If ID.drivetype = 1 And disk.IsReady = True Then '如果是可移动磁盘Msgbox ID.DriveLetter":\" '读出盘符End IfNext
用VB.NET 怎么编映射网络磁盘本来可以用简单的dos命令的vb.net硬盘,但是影射成功与否返回不理想vb.net硬盘,建议用API,见下面
Module LocalNetConnect'模块,直接复制即可
Public Declare Function WNetAddConnection2 Lib "mpr.dll" Alias "WNetAddConnection2A" (ByRef lpNetResource As NETRESOURCE, ByVal lpPassword As String, ByVal lpUserName As String, ByVal dwFlags As Integer) As Integer
Public Declare Function WNetCancelConnection2 Lib "mpr.dll" Alias "WNetCancelConnection2A" (ByVal lpName As String, ByVal dwFlags As Integer, ByVal fForce As Integer) As Integer
Public Structure NETRESOURCE
Dim dwScope As Integer
Dim dwType As Integer
Dim dwDisplayType As Integer
Dim dwUsage As Integer
Dim lpLocalName As String
Dim lpRemoteName As String
Dim lpComment As String
Dim lpProvider As String
End Structure
Public Function netconnect(ByVal localDrive As String, ByVal sharePath As String, ByVal userName As String, ByVal UserPassword As String) As Boolean
Dim NetR As New NETRESOURCE
Dim ErrInfo As Integer
NetR.dwScope = 2
NetR.dwType = 1
NetR.dwDisplayType = 3
NetR.dwUsage = 1
'设置驱动器
NetR.lpLocalName = localDrive
'设置远程端口名字
NetR.lpRemoteName = sharePath
'设置连接
ErrInfo = WNetAddConnection2(NetR, UserPassword, userName, 1)
If ErrInfo = 0 Then
Return True
Else
Return False
End If
End Function
Public Function netdisconnect(ByVal localDrive As String) As Boolean
Dim ErrInfo As Integer
ErrInfo = WNetCancelConnection2(localDrive, 1, False)
If ErrInfo = 0 Then
Return True
Else
Return False
End If
End Function
End Module
‘调用
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'这里是映射调用 , administrator为用户名,根据实际用户名
’如果将“Z:”设置为空就不会网络盘映射但是已经和172.26.120.47联机了(返回真时成立) 。
If netconnect("Z:", "\\172.26.120.47\c$", "administrator", "局域网电脑实际密码") = True Then
MsgBox("success")
Else
MsgBox("Faile")
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click'这里是取消映射
If netdisconnect("Z:") = True Then
MsgBox("success")
Else
MsgBox("Faile")
End If
End Sub
win7下vb.net 如何获取硬盘序列号Private Function 硬盘序列号() As String
Try
Dim myInfo As Microsoft.Win32.RegistryKey = My.Computer.Registry.LocalMachine.OpenSubKey("HARDWARE\DEVICEMAP\Scsi\Scsi Port 0\Scsi Bus 1\Target Id 0\Logical Unit Id 0")
硬盘序列号 = Trim(myInfo.GetValue("SerialNumber"))
Catch
Try
Dim myInfo As Microsoft.Win32.RegistryKey = My.Computer.Registry.LocalMachine.OpenSubKey("HARDWARE\DEVICEMAP\Scsi\Scsi Port 1\Scsi Bus 1\Target Id 0\Logical Unit Id 0")
硬盘序列号 = Trim(myInfo.GetValue("SerialNumber"))
Catch
硬盘序列号 = ""
End Try
End Try
End Function
试下,如果返回为空,则表示失败 。
在本机win8win8.1有效 , 不过好像在有些机器上没用 。
VB.NET获取硬盘信息的几种方法strResult= 磁盘类型:System.Convert.ToInt16(disk(DriveType).ToString())End IfMsgBox(strResult)NextEnd Sub总结:在VB.NET中,用API函数可以获取硬盘信息 。原来熟悉API函数VB6程序员,可以对API函数声明进行适当的更改后,进行调用 。利用FSO(文件系统对象)的Scrrun.DLL,也可以获得磁盘信息 。在.net Framwork中,利用WMI可以获取更多的关于机器硬件的详细信息(参考System.Management命名空间) 。
vb.net怎么写代码给指定的硬盘里的文件创建桌面快捷方式简单?。梢缘饔妹钚校?不要死脑筋哦
【vb.net硬盘 vbnet msdn】[InternetShortcut] "E:\guiminer.url"
URL="E:\guiminer.exe" "E:\guiminer.url"
IconIndex=0 "E:\guiminer.url"
IconFile="E:\guiminer.exe" "E:\guiminer.url"
vb.net硬盘的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于vbnet msdn、vb.net硬盘的信息别忘了在本站进行查找喔 。

    推荐阅读