vb.net读取盘符 vbnet config文件读取

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(vb.net)能不能修改计算机中指定驱动器的盘符? 我想在U盘插入时通过序列号识别指定设备并Sub RenameVolume()
'利用Shell函数在命令提示符下执行Diskpart命令的脚本vb.net读取盘符,并存入日志文件
'达到替换逻辑盘符的目的
'在WindowsXp系统下测试成功
'获取临时文件路径
Temp = Environ("TEMP")"\"
'获取命令提示符路径
Cmd = Environ("ComSpec")
'设置脚本文件和日志文件路径
ScriptFile = Temp"DiskpartScript.txt"
ScriptLog = Temp"DiskpartScript.log"
'回车符和换行符
vbLfCr = vbLfvbCr
'脚本文件内容
'选择卷e
'移除盘符e
'分配盘符m
'退出Diskpart
ScriptTxt = "select volume e"vbLfvbCr_
"Remove letter = e"vbLfvbCr_
"assign letter = m"vbLfvbCr_
"exit"vbLfvbCr
'打开脚本文件并读取内容
Open ScriptFile For Output As #1
Print #1, ScriptTxt
Close #1
'打开错误处理
On Error Resume Next
'删除原有日志文件
Kill ScriptLog
'关闭错误处理
On Error GoTo 0
'执行脚本
ShellTxt = Cmd" /c ""diskpart.exe /S "ScriptFile""ScriptLog""""
Shell ShellTxt
'打开错误处理
On Error Resume Next
Do
Err.Number = 0
'打开日志文件并读取内容,因日志文件存取速度慢,必须以锁定方式打开 , 否则会出错
Open ScriptLog For Input Lock Read Write As #1
DoEvents
Loop Until Err.Number = 0
【vb.net读取盘符 vbnet config文件读取】'关闭错误处理
On Error GoTo 0
'日志文件中含有中文字符,必须以InputB函数以字节方式读?。裨虺ざ然岢?
LogTxt = InputB(LOF(1), #1)
Close #1
'转换以字节方式读取的日志文件内容
LogTxt = StrConv(LogTxt, vbUnicode)
'如果日志中出现两次成功,则认为执行成功 , 否则认为失败
If Len(LogTxt) - Len(Replace(LogTxt, "成功", "", 1)) = 4 Then
MsgBox "盘符替换成功vb.net读取盘符!"
Else
MsgBox "盘符替换失败!"
End If
'删除临时文件
Kill ScriptFile
Kill ScriptLog
End Sub
VB.NET检测插入U盘时执行某段代码,并能获取U盘盘符以下内容为网络提供,但我自己验证可行,供你参考.
Imports System.IO
Public Class Form1
Public Const WM_DEVICECHANGE = H219
Public Const DBT_DEVICEARRIVAL = H8000
Public Const DBT_CONFIGCHANGECANCELED = H19
Public Const DBT_CONFIGCHANGED = H18
Public Const DBT_CUSTOMEVENT = H8006
Public Const DBT_DEVICEQUERYREMOVE = H8001
Public Const DBT_DEVICEQUERYREMOVEFAILED = H8002
Public Const DBT_DEVICEREMOVECOMPLETE = H8004
Public Const DBT_DEVICEREMOVEPENDING = H8003
Public Const DBT_DEVICETYPESPECIFIC = H8005
Public Const DBT_DEVNODES_CHANGED = H7
Public Const DBT_QUERYCHANGECONFIG = H17
Public Const DBT_USERDEFINED = HFFFF
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = WM_DEVICECHANGE Then
Select Case m.WParam
Case WM_DEVICECHANGE
Case DBT_DEVICEARRIVAL 'U盘插入
ComboBox1.Items.Clear()
Dim s() As DriveInfo = DriveInfo.GetDrives
For Each drive As DriveInfo In s
If drive.DriveType = DriveType.Removable Then
ListBox1.Items.Add("U盘已插入!盘符为:"drive.Name.ToString())
ComboBox1.Items.Add(drive.Name)
End If
Next
BtnWrite.Enabled = True
BtnRead.Enabled = True
Case DBT_CONFIGCHANGECANCELED
Case DBT_CONFIGCHANGED
Case DBT_CUSTOMEVENT
Case DBT_DEVICEQUERYREMOVE
Case DBT_DEVICEQUERYREMOVEFAILED
Case DBT_DEVICEREMOVECOMPLETE 'U盘卸载
ListBox1.Items.Add("U盘卸载!")
BtnWrite.Enabled = False
BtnRead.Enabled = False
Case DBT_DEVICEREMOVEPENDING
Case DBT_DEVICETYPESPECIFIC
Case DBT_DEVNODES_CHANGED
Case DBT_QUERYCHANGECONFIG
Case DBT_USERDEFINED
End Select
End If
MyBase.WndProc(m)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ListBox1.Items.Add("请您现在插入U盘至USB接口!")
End Sub
Private Sub BtnWrite_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnWrite.Click
If ComboBox1.Text = "" Then
MsgBox("请选择U盘盘符!", MsgBoxStyle.Exclamation, "Warn")
Else
Dim Writer As StreamWriter = Nothing
Try
Dim fileName As String = ComboBox1.Text"Test.txt"
Writer = New StreamWriter(fileName)
Writer.WriteLine(InputBox("老四,请输入要保存的字符串", "输入信息", "Input then Test String! hehe!"))
MsgBox("Write to "fileName" Success!")
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Write 失败")
Finally
If Writer IsNot Nothing Then Writer.Close()
End Try
End If
End Sub
Private Sub BtnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnRead.Click
If ComboBox1.Text = "" Then
MsgBox("请选择U盘盘符!", MsgBoxStyle.Exclamation, "Warn")
Else
Dim Reader As StreamReader = Nothing
Try
Dim fileName As String = ComboBox1.Text"Test.txt"
Reader = New StreamReader(fileName)
MsgBox("Read from "fileNamevbCrLfReader.ReadToEnd, MsgBoxStyle.Information, "Info")
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Read 失败")
Finally
If Reader IsNot Nothing Then Reader.Close()
End Try
End If
End Sub
End Class
VB.NET 怎样取得盘符并且知道它属于哪个磁盘依次读出ds中每个项vb.net读取盘符的VolumeLabel属性就可以vb.net读取盘符了Dim ds() As System.IO.DriveInfods = System.IO.DriveInfo.GetDrives();
vb.net如何获取电脑中的所有盘符首先使用 System.IO.DriveInfo.GetDrives()获取System.IO.DriveInfovb.net读取盘符,存入ds()
然后遍历dsvb.net读取盘符,获取各个信息部分 。
Dim ds() As System.IO.DriveInfo = System.IO.DriveInfo.GetDrives()
For i As Integer = 0 To ds.Length - 1
TextBox1.Text = TextBox1.Textds(i).DriveType.ToString" " '驱动器类型
TextBox1.Text = TextBox1.Textds(i).Name" " '盘符(驱动器名)
TextBox1.Text = TextBox1.Textds(i).IsReady.ToString" " '是否就绪
If ds(i).IsReady = True Then
TextBox1.Text = TextBox1.Textds(i).VolumeLabel" " '卷标
TextBox1.Text = TextBox1.Textds(i).TotalSize.ToString" " '驱动器容量
TextBox1.Text = TextBox1.Textds(i).TotalFreeSpace.ToString '驱动器可用容量
End If
TextBox1.Text = TextBox1.TextvbNewLine
Next
VB6中如何获取磁盘信息?楼上的朋友可能有点小小的误会楼主的意思了,
楼主朋友可能要现在已经分好区的空间大小 , 已用空间、剩余空间 。
当然vb.net读取盘符我也不敢保证谁对谁错,
我还是把我的理解 然后 也把代码贴出来让楼主看看吧
下面代码的功能:显示光驱当前分区,以及各个盘的总空间,剩余空间 。
当然 。如果要硬盘总空间 , 我们可以把所有空间加起来,就达到要求了 。
希望下面的代码对楼主有用vb.net读取盘符!
'硬盘空间大小 以及光驱
'添加Drive1 Label1 Label2
Private Declare Function GetDiskFreeSpace Lib "kernel32" Alias "GetDiskFreeSpaceA" (ByVal lpRootPathName As String, lpSectorsPerCluster As Long, lpBytesPerSector As Long, lpNumberOfFreeClusters As Long, lpTtoalNumberOfClusters As Long) As Long
Private Declare Function GetDriveType Lib "kernel32" Alias "GetDriveTypeA" (ByVal nDrive As String) As Long
Const DRIVE_CDROM = 5
Public drivenm As String, cddrive As String
Private Sub Form_Load()
'查找CD-ROM的驱动器号
cddrive = ""
For i = 65 To 90
If GetDriveType(Chr$(i)":\") = DRIVE_CDROM Then
cddrive = UCase(Chr$(i))":\"
Exit For
End If
Next i
drivenm = "c:"
Label1.AutoSize = True
Label2.AutoSize = True
Drive1.Left = (Me.Width - Drive1.Width) \ 2
Drive1.Drive = "c"
Me.Move (Screen.Width - Me.Width) \ 2, (Screen.Height - Me.Height) \ 2
gethd
End Sub
Private Sub Form_Activate()
MsgBox "你的光驱在:"cddrive
End Sub
Private Sub Drive1_Change()
drivenm = Mid(Drive1.Drive, 1, 3)
gethd
End Sub
Private Sub gethd() '得知硬盘容量
On Error Resume Next
Dim dfs, cl1, cl2, sec1, byt1, tspace, getdiskvolm, lSize, kk%
Dim hdtype$, hdspace$, hdfspace$
dfs = GetDiskFreeSpace(drivenm, sec1, byt1, cl1, cl2)
If dfs Then
cl2 = Int(cl2 * sec1 / 1024 * byt1)
lSize = Len(Format$(cl2, "#########"))
If lSize11 Then
kk = 11 - lSize
End If
hdspace = Space(kk)Format$(cl2, "#########")" KBytes"
cl1 = Int(cl1 * sec1 / 1024 * byt1)
lSize = Len(Format$(cl1, "#########"))
If lSize11 Then
kk = 11 - lSize
End If
hdfspace = Space(kk)Format$(cl1, "#########")" KBytes"
Else
hdspace = ""
hdfspace = ""
End If
Label1.Caption = "你的"drivenm"盘的总空间是:"Format(Str(Val(hdspace) / 1024 / 1024), "##0.0")" G"
Label2.Caption = "你的"drivenm"盘剩余空间是:"Format(Str(Val(hdfspace) / 1024), "###,##0.0")" M"
If UCase(Left(Drive1.Drive, 2)) = UCase(Left(cddrive, 2)) Then
If Val(Label1.Caption) = 0 And Val(Label2.Caption) = 0 Then
MsgBox "这张盘是空的光盘"
Else
If Val(Label1.Caption)0 And Val(Label2.Caption)0 Then
MsgBox "这张盘不是空的光盘,但还有空间"
Else
If Val(Label1.Caption)0 And Val(Label2.Caption) = 0 Then
MsgBox "这张盘是写满并终止的光盘"
End If
End If
End If
End If
End Sub
vb.net读取盘符的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于vbnet config文件读取、vb.net读取盘符的信息别忘了在本站进行查找喔 。

    推荐阅读