VB获取文件路径是怎么样的?

VB获取文件路径是怎么样的?,第1张

VB2008测试通过 Imports System Imports System.IO Imports System.Threading Imports System.Windows.Forms\x0d\x0a Public Class FrmMain\x0d\x0a\x0d\x0aDim newSearcher As getFileByName\x0d\x0a\x0d\x0a Private Sub FrmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load\x0d\x0a\x0d\x0arball.Checked = True\x0d\x0a\x0d\x0atbpath.Text = ""\x0d\x0a\x0d\x0atbpath.Enabled = False\x0d\x0a\x0d\x0abtndlg.Enabled = False\x0d\x0a\x0d\x0aEnd Sub\x0d\x0a\x0d\x0a Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click\x0d\x0a\x0d\x0atv.Nodes.Clear()\x0d\x0a\x0d\x0abtnOK.Enabled = False\x0d\x0a\x0d\x0abtnExit.Enabled = False\x0d\x0a\x0d\x0aIf rball.Checked Then\x0d\x0a\x0d\x0arun()\x0d\x0a\x0d\x0aElse\x0d\x0a\x0d\x0aIf tbpath.Text = "" Then\x0d\x0a\x0d\x0aMsgBox("请输入或者选择一个路径")\x0d\x0a\x0d\x0aExit Sub\x0d\x0a\x0d\x0aElse\x0d\x0a\x0d\x0arun(tbpath.Text)\x0d\x0a\x0d\x0aEnd If\x0d\x0a\x0d\x0aEnd If\x0d\x0a\x0d\x0aEnd Sub\x0d\x0a\x0d\x0a Private Sub btndlg_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndlg.Click\x0d\x0a\x0d\x0afbdlg.RootFolder = Environment.SpecialFolder.MyComputer\x0d\x0a\x0d\x0afbdlg.ShowDialog()\x0d\x0a\x0d\x0atbpath.Text = fbdlg.SelectedPath\x0d\x0a\x0d\x0aEnd Sub\x0d\x0a\x0d\x0a Private Sub rbchs_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbchs.CheckedChanged\x0d\x0a\x0d\x0aIf rbchs.Checked = True Then\x0d\x0a\x0d\x0atbpath.Enabled = True\x0d\x0a\x0d\x0abtndlg.Enabled = True\x0d\x0a\x0d\x0aElse\x0d\x0a\x0d\x0atbpath.Enabled = False\x0d\x0a\x0d\x0abtndlg.Enabled = False\x0d\x0a\x0d\x0aEnd If\x0d\x0a\x0d\x0aEnd Sub\x0d\x0a\x0d\x0a Private Sub run()\x0d\x0a\x0d\x0aIf tbfileName.Text = "" Then\x0d\x0a\x0d\x0aMsgBox("请输入文件名称")\x0d\x0a\x0d\x0abtnOK.Enabled = True\x0d\x0a\x0d\x0abtnExit.Enabled = True\x0d\x0a\x0d\x0aExit Sub\x0d\x0a\x0d\x0aElse\x0d\x0a\x0d\x0anewSearcher = New getFileByName(tbfileName.Text)\x0d\x0a\x0d\x0anewSearcher.GetDrivers()\x0d\x0a\x0d\x0atmr.Enabled = True\x0d\x0a\x0d\x0atmr.Interval = 10\x0d\x0a\x0d\x0aEnd If\x0d\x0a\x0d\x0aEnd Sub\x0d\x0a\x0d\x0a Private Sub run(ByVal path As String)\x0d\x0a\x0d\x0aIf tbfileName.Text = "" Then\x0d\x0a\x0d\x0aMsgBox("请输入文件名称")\x0d\x0a\x0d\x0abtnOK.Enabled = True\x0d\x0a\x0d\x0abtnExit.Enabled = True\x0d\x0a\x0d\x0aExit Sub\x0d\x0a\x0d\x0aElse\x0d\x0a\x0d\x0anewSearcher = New getFileByName(tbfileName.Text)\x0d\x0a\x0d\x0anewSearcher.SearchFolder(path)\x0d\x0a\x0d\x0atmr.Enabled = True\x0d\x0a\x0d\x0atmr.Interval = 50\x0d\x0a\x0d\x0aEnd If\x0d\x0a\x0d\x0aEnd Sub\x0d\x0a\x0d\x0a Private Sub tmr_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmr.Tick\x0d\x0a\x0d\x0aLblshow.Text = newSearcher.currentPath\x0d\x0a\x0d\x0aIf newSearcher.cntThread = 0 And newSearcher.isDone Then\x0d\x0a\x0d\x0a'MsgBox(newSearcher.getFile.ToString)\x0d\x0a\x0d\x0a'MsgBox("全部线程结束")\x0d\x0a\x0d\x0aFor Each Str As String In newSearcher.getFile\x0d\x0a\x0d\x0atv.Nodes.Add(Str)\x0d\x0a\x0d\x0aNext\x0d\x0a\x0d\x0a'For i As Integer = 0 To newSearcher.getFile.Length - 1\x0d\x0a\x0d\x0a'tv.Nodes.Add(newSearcher.getFile(i))\x0d\x0a\x0d\x0a'Next\x0d\x0a\x0d\x0atmr.Enabled = False\x0d\x0a\x0d\x0abtnExit.Enabled = True\x0d\x0a\x0d\x0abtnOK.Enabled = True\x0d\x0a\x0d\x0aEnd If\x0d\x0a\x0d\x0aMy.Application.DoEvents()\x0d\x0a\x0d\x0aEnd Sub\x0d\x0a\x0d\x0a Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click\x0d\x0a\x0d\x0aMe.Close()\x0d\x0a\x0d\x0aEnd Sub End Class

给你个VB遍历文件夹的代码吧

Private

Declare

Function

FindFirstFile

Lib

"kernel32"

Alias

"FindFirstFileA"

(ByVal

lpFileName

As

String,

lpFindFileData

As

WIN32_FIND_DATA)

As

Long

'查找下一个文件的API

Private

Declare

Function

FindNextFile

Lib

"kernel32"

Alias

"FindNextFileA"

(ByVal

hFindFile

As

Long,

lpFindFileData

As

WIN32_FIND_DATA)

As

Long

'获取文件属性的API

Private

Declare

Function

GetFileAttributes

Lib

"kernel32"

Alias

"GetFileAttributesA"

(ByVal

lpFileName

As

String)

As

Long

'关闭查找文件的API

Private

Declare

Function

FindClose

Lib

"kernel32"

(ByVal

hFindFile

As

Long)

As

Long

Const

MAX_PATH

=

260

Const

MAXDWORD

=

&HFFFF

Const

FILE_ATTRIBUTE_DIRECTORY

=

&H10

Private

Type

FILETIME

dwLowDateTime

As

Long

dwHighDateTime

As

Long

End

Type

Dim

tempstr

As

String

'定义类(用于查找文件)

Private

Type

WIN32_FIND_DATA

dwFileAttributes

As

Long

ftCreationTime

As

FILETIME

ftLastACCESSTime

As

FILETIME

ftLastWriteTime

As

FILETIME

nFileSizeHigh

As

Long

nFileSizeLow

As

Long

dwReserved0

As

Long

dwReserved1

As

Long

cFileName

As

String

*

MAX_PATH

cAlternate

As

String

*

14

End

Type

Dim

filecount

As

Integer

Dim

dirs()

As

String

Dim

curr

As

Long

Dim

ss()

As

String

Private

Sub

Command1_Click()

tempstr

=

"c:"

searchdir

tempstr

filecount

=

0

End

Sub

Public

Function

searchdir(path

As

String)

Dim

WFD

As

WIN32_FIND_DATA

Dim

i

As

Long

Dim

temp

As

String

Dim

h

As

Long

Dim

zhaodao

As

Long

Dim

iindex

As

Integer

Dim

dirs()

As

String

Dim

l

As

Long

zhaodao

=

1

h

=

FindFirstFile(path

&

"\*.*",

WFD)

If

h

<>

-1

Then

While

zhaodao

zhaodao

=

1

temp

=

Left(WFD.cFileName,

InStr(WFD.cFileName,

Chr$(0))

-

1)

If

temp

<>

"."

And

temp

<>

".."

Then

If

WFD.dwFileAttributes

And

vbDirectory

Then

ReDim

Preserve

dirs(iindex)

dirs(iindex)

=

path

&

"\"

&

temp

iindex

=

iindex

+

1

ReDim

Preserve

ss(filecount)

ss(filecount)

=

path

&

"\"

&

temp

filecount

=

filecount

+

1

End

If

End

If

zhaodao

=

FindNextFile(h,

WFD)

Wend

End

If

FindClose

(h)

If

iindex

>

0

Then

For

i

=

0

To

iindex

-

1

Call

searchdir(dirs(i))

Next

i

End

If

End

Function


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/11974220.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-20
下一篇 2023-05-20

发表评论

登录后才能评论

评论列表(0条)

保存