为什么我的VB代码不起作用? [英] Why isn't my VB code working?
问题描述
目前我还不知道我的代码有什么问题.我正在尝试实现一种获取文件方法,但是由于某种原因,扫描仪仍只能进入一个级别,而不进入子文件夹.
这是我要改进的按钮开始代码.....
At this point I have no idea what''s wrong with my code. I am trying to acheive a get file method but for some reason the scanner is still only going one level and not into the subfolders.
Here is the button start code I''m trying to improve.....
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim searchForTheseFiles() As String = TextBox1.Text.ToUpper.Split(Environment.NewLine)
Dim fileList As New List(Of String)(searchForTheseFiles)
Dim myDir As New DirectoryInfo(CheckedComboBoxEdit1.Text)
Try
For Each foundFile As FileInfo In myDir.GetFiles("*.*", SearchOption.AllDirectories)
If fileList.Contains(foundFile.Name.ToUpper) Then
ListBox3.Text &= Environment.NewLine & foundFile.FullName
My.Computer.Audio.Play(My.Resources.fat_n_soft_button_4, AudioPlayMode.Background)
End If
Next
Catch ex As UnauthorizedAccessException
Debug.WriteLine(String.Format("Could not access directory '{0}'.", myDir.FullName))
End Try
:( :( :(我现在已经很久了,感觉很蠢....idk请给hlp!:(
:( :( :( I have been at this now to long and feel stupid.... idk pls hlp! :(
推荐答案
不要放一个在循环结构的开始处调用DirectoryInfo.GetFiles()
尝试尝试此操作;
Don''t put aDirectoryInfo.GetFiles()
call at the start of a loop construct try this instead;
Dim searchForTheseFiles() As String = TextBox1.Text.ToUpper.Split(Environment.NewLine)
Dim fileList As New List(Of String)(searchForTheseFiles)
Dim myDir As New DirectoryInfo(CheckedComboBoxEdit1.Text)
Try
Dim files() As FileInfo = myDir.GetFiles("*.*", SearchOption.AllDirectories)
For Each foundFile As FileInfo In files
If fileList.Contains(foundFile.Name.ToUpper) Then
ListBox3.Text &= Environment.NewLine & foundFile.FullName
My.Computer.Audio.Play(My.Resources.fat_n_soft_button_4, AudioPlayMode.Background)
End If
Next
Catch ex As UnauthorizedAccessException
Debug.WriteLine(String.Format("Could not access directory ''{0}''.", myDir.FullName))
End Try
好的机会是,您遇到访问被拒绝的错误,似乎AllDirectories搜索选项是如此愚蠢,以至于遇到目录您无法访问且不返回任何内容而不是跳过该目录时,它只会抛出一个错误.因此,您要么需要学习如何设置访问权限,要么需要实现自己的递归搜索功能,就我个人而言,我将自己做搜索功能,然后您可以检查是否有权访问目录,如果没有访问权限,则跳过该目录,这样您就不会尝试访问隐藏的系统文件夹.
OK chances are you are coming up against an access denied error, it would appear that the AllDirectories search option is so dumb that it just throws an error when it encounters a directory you can''t access and returns nothing instead of just skipping over it. So you either need to learn how to set access permissions or implement your own recursive search function, personally I would go with doing your own search function as you can then check if you have access to a directory and skip it if you don''t, this way you are not trying to gain access to hidden system folders.
这篇关于为什么我的VB代码不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!