调用模块分配有效路径 [英] Calling a module & assigning a valid path
问题描述
我不熟悉使用VB或任何编程语言编写模块的知识,需要了解如何在程序中调用模块以及如何分配有效路径.
我不确定该怎么做,也没有得到太多反馈.有人告诉我此代码有效,但是当我问这个人如何使用它时,他们只是问另一个很烦人的问题.
知道的人可以给我一些答案吗?提前谢谢.
当用户单击我程序中的开始扫描按钮时,模块代码需要工作:
I am new to using modules in VB or any programing language and need to know how a module is called in a program and how to assign a valid path.
I am not sure how to do both and have not gotton much feed back. Someone has told me this code works but when I ask the person how to use it they just ask another question which is quite annoying.
Can someone that knows please give me some answers? Thanks in advance.
The module code needs to work when the user clicks the start scan button in my program:
Module BrowseFoldersEx
Sub main()
Dim direc As New DirectoryInfo("C:\abhishek")
If direc.Exists = False Then
MsgBox("The path does not exist. Please specify a correct path")
Exit Sub
End If
Console.WriteLine("Folders are : ")
RepeatDir(direc)
Console.ReadLine()
End Sub
Function RepeatDir(ByVal drr As DirectoryInfo)
Dim fls As DirectoryInfo
For Each fls In drr.GetDirectories()
Console.WriteLine(fls.Name)
RepeatDir(fls)
Console.WriteLine("Checking for Files:")
RepeatFiles(fls)
Next
End Function
Function RepeatFiles(ByVal dirs As DirectoryInfo)
Dim fl As FileInfo
For Each fl In dirs.GetFiles()
Console.WriteLine(fl.Name)
Next
If fl Is Nothing Then
Console.WriteLine("No files are present in this folder " & dirs.Name)
End If
End Function
End Module
如果有人可以解决此代码中需要解决的任何问题,并简单地详细说明如何应用此模块或将其调用到有效路径,则将彻底杜绝我的麻烦.
我将对愿意回答我所提出的完整问题的人给出5分.
If someone can fix whatever needs to be fixed in this code and explain in simple detail how to apply this module or call it to a valid path it would put an end to my trouble once and for all.
I will give a full 5 points to anyone willing to atleast help with the full question I have asked.
推荐答案
您在这里拥有的控制台程序可以正常工作,但是您还需要在运行控制台应用程序时添加Imports System.IO
,如果您更改Dim direc As New DirectoryInfo("C:\abhishek")
行并在其中放置有效路径并更改RepeatDir函数,则它将从运行Sub main()
开始;
What you have here is a console program which does work but you would also need to addImports System.IO
when you run a console application it begins by running theSub main()
if you change the lineDim direc As New DirectoryInfo("C:\abhishek")
and put a valid path in here and make a change to the RepeatDir function;
Function RepeatDir(ByVal drr As DirectoryInfo)
Dim directoryListing() As DirectoryInfo = drr.GetDirectories()
For Each fls As DirectoryInfo In directoryListing
Try
Console.WriteLine(fls.FullName)
RepeatDir(fls)
Console.WriteLine("Checking for Files:")
RepeatFiles(fls)
Catch ex As Exception
Console.WriteLine("Unable to access directory:" & fls.FullName)
Console.ReadLine()
End Try
Next
End Function
现在,它将跳过它无法访问的所有文件夹.
如果要在Winform应用程序中使用此功能,请使用RepeatDir
和RepeatFiles
函数并将它们复制到表单中,然后在按钮中单击开始扫描"按钮的处理程序,完成此操作后,将Sub main()
的内容放入其中用其他方式替换所有Console.WriteLine()
调用以显示信息,例如追加到文本框或将项目添加到列表框.
请记住将您的路径放入Dim direc As New DirectoryInfo("C:\")
中,这应该很好.
您应该以类似以下内容结束:
It will now skip over any folders that it can''t access.
If you want to use this in a winform application take the RepeatDir
and RepeatFiles
functions and copy them into your form then in the button click handler for your start scan button put the contents of the Sub main()
once you have done this replace all the Console.WriteLine()
calls with some other way to display the information such as appending to a textbox or add items to a listbox.
Remember to put your path into Dim direc As New DirectoryInfo("C:\")
and it should be good to go.
You should end up with something like;
Imports System.IO
Public Class Form1
Private Sub searchButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles searchButton.Click
Dim direc As New DirectoryInfo("C:\") ''Put your path here.
If direc.Exists = False Then
MsgBox("The path does not exist. Please specify a correct path")
Exit Sub
End If
ListBox1.Items.Add("Folders are : ")
Application.DoEvents()
RepeatDir(direc)
End Sub
Function RepeatDir(ByVal drr As DirectoryInfo)
Dim directoryListing() As DirectoryInfo = drr.GetDirectories()
For Each fls As DirectoryInfo In directoryListing
Try
ListBox1.Items.Add(fls.FullName)
RepeatDir(fls)
ListBox1.Items.Add("Checking for Files:")
RepeatFiles(fls)
Catch ex As Exception
ListBox1.Items.Add("Unable to access directory:" & fls.FullName)
End Try
Application.DoEvents()
Next
End Function
Function RepeatFiles(ByVal dirs As DirectoryInfo)
Dim fileListing() As FileInfo = dirs.GetFiles()
If fileListing.Length > 0 Then
For Each fl As FileInfo In fileListing
ListBox1.Items.Add(fl.FullName)
Next
Else
ListBox1.Items.Add("No files are present in this folder " & dirs.Name)
End If
Application.DoEvents()
End Function
End Class
此代码有效,您可以像这样调用它:BrowseFoldersEx.main()
如果C:\abhishek
不是有效路径,请将其更改为有效值,例如C:\
.
请记住,此模块在控制台窗口中显示结果,因此,如果要在表单上显示结果,则需要修改此代码.
This code works, and you can call it like this:BrowseFoldersEx.main()
IfC:\abhishek
is not a valid path, change it to something valid, likeC:\
.
Have in mind that this module prints results in the console window, so if you want to present results on your form, this code needs modifications.
这篇关于调用模块分配有效路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!