473,799 Members | 3,832 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to I put files from Directory into listbox, then open files from listbox to RTB?

24 New Member
My knowledge of VB.net is limited (only have been reading from the book)

so what i need to do for my project is to populate a listbox with files from a certain directory (C:\folder)

then once it's populated, i can select an item and it will open up in a rich text box.

This is what i have so far for the active directory > list box:

Expand|Select|Wrap|Line Numbers
  1.         Dim di As New IO.DirectoryInfo("C:\Documents and Settings\user\Desktop\folder")
  2.         Dim diar1 As IO.FileInfo() = di.GetFiles()
  3.         Dim dra As IO.FileInfo
  4.         Dim files As String
  5.  
  6.         'list the names of all files in the specified directory
  7.         For Each dra In diar1
  8.             Dim shortName As String
  9.             shortName = dra.Name.Replace("931pros-", "")
  10.             ListBox2.Items.Add(shortName)
  11.         Next
  12.  
Thanks!
Mar 11 '11 #1
2 5831
ygs1234
24 New Member
I found something like this, but it didn't work.

http://www.vbdotnetforums.com/listvi...directory.html
Mar 11 '11 #2
piyushagrawal
1 New Member
  1. 'Create a form with a command button (command1), a list box (list1)
  2. 'and four text boxes (text1, text2, text3 and text4).
  3. 'Type in the first textbox a startingpath like c:\
  4. 'and in the second textbox you put a pattern like *.* or *.txt
Expand|Select|Wrap|Line Numbers
  1. Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
  2. Private Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
  3. Private Declare Function GetFileAttributes Lib "kernel32" Alias "GetFileAttributesA" (ByVal lpFileName As String) As Long
  4. Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
  5.  
  6. Const MAX_PATH = 260
  7. Const MAXDWORD = &HFFFF
  8. Const INVALID_HANDLE_VALUE = -1
  9. Const FILE_ATTRIBUTE_ARCHIVE = &H20
  10. Const FILE_ATTRIBUTE_DIRECTORY = &H10
  11. Const FILE_ATTRIBUTE_HIDDEN = &H2
  12. Const FILE_ATTRIBUTE_NORMAL = &H80
  13. Const FILE_ATTRIBUTE_READONLY = &H1
  14. Const FILE_ATTRIBUTE_SYSTEM = &H4
  15. Const FILE_ATTRIBUTE_TEMPORARY = &H100
  16.  
  17. Private Type FILETIME
  18.     dwLowDateTime As Long
  19.     dwHighDateTime As Long
  20. End Type
  21.  
  22. Private Type WIN32_FIND_DATA
  23.     dwFileAttributes As Long
  24.     ftCreationTime As FILETIME
  25.     ftLastAccessTime As FILETIME
  26.     ftLastWriteTime As FILETIME
  27.     nFileSizeHigh As Long
  28.     nFileSizeLow As Long
  29.     dwReserved0 As Long
  30.     dwReserved1 As Long
  31.     cFileName As String * MAX_PATH
  32.     cAlternate As String * 14
  33. End Type
  34. Function StripNulls(OriginalStr As String) As String
  35.     If (InStr(OriginalStr, Chr(0)) > 0) Then
  36.         OriginalStr = Left(OriginalStr, InStr(OriginalStr, Chr(0)) - 1)
  37.     End If
  38.     StripNulls = OriginalStr
  39. End Function
  40.  
  41. Function FindFilesAPI(path As String, SearchStr As String, FileCount As Integer, DirCount As Integer)
  42.     Dim FileName As String ' Walking filename variable...
  43.     Dim DirName As String ' SubDirectory Name
  44.     Dim dirNames() As String ' Buffer for directory name entries
  45.     Dim nDir As Integer ' Number of directories in this path
  46.     Dim i As Integer ' For-loop counter...
  47.     Dim hSearch As Long ' Search Handle
  48.     Dim WFD As WIN32_FIND_DATA
  49.     Dim Cont As Integer
  50.     If Right(path, 1) <> "\" Then path = path & "\"
  51.     ' Search for subdirectories.
  52.     nDir = 0
  53.     ReDim dirNames(nDir)
  54.     Cont = True
  55.     hSearch = FindFirstFile(path & "*", WFD)
  56.     If hSearch <> INVALID_HANDLE_VALUE Then
  57.         Do While Cont
  58.         DirName = StripNulls(WFD.cFileName)
  59.         ' Ignore the current and encompassing directories.
  60.         If (DirName <> ".") And (DirName <> "..") Then
  61.             ' Check for directory with bitwise comparison.
  62.             If GetFileAttributes(path & DirName) And FILE_ATTRIBUTE_DIRECTORY Then
  63.                 dirNames(nDir) = DirName
  64.                 DirCount = DirCount + 1
  65.                 nDir = nDir + 1
  66.                 ReDim Preserve dirNames(nDir)
  67.             End If
  68.         End If
  69.         Cont = FindNextFile(hSearch, WFD) 'Get next subdirectory.
  70.         Loop
  71.         Cont = FindClose(hSearch)
  72.     End If
  73.     ' Walk through this directory and sum file sizes.
  74.     hSearch = FindFirstFile(path & SearchStr, WFD)
  75.     Cont = True
  76.     If hSearch <> INVALID_HANDLE_VALUE Then
  77.         While Cont
  78.             FileName = StripNulls(WFD.cFileName)
  79.             If (FileName <> ".") And (FileName <> "..") Then
  80.                 FindFilesAPI = FindFilesAPI + (WFD.nFileSizeHigh * MAXDWORD) + WFD.nFileSizeLow
  81.                 FileCount = FileCount + 1
  82.                 List1.AddItem path & FileName
  83.             End If
  84.             Cont = FindNextFile(hSearch, WFD) ' Get next file
  85.         Wend
  86.         Cont = FindClose(hSearch)
  87.     End If
  88.     ' If there are sub-directories...
  89.     If nDir > 0 Then
  90.         ' Recursively walk into them...
  91.         For i = 0 To nDir - 1
  92.             FindFilesAPI = FindFilesAPI + FindFilesAPI(path & dirNames(i) & "\", SearchStr, FileCount, DirCount)
  93.         Next i
  94.    End If
  95. End Function
  96. Sub Command1_Click()
  97.     Dim SearchPath As String, FindStr As String
  98.     Dim FileSize As Long
  99.     Dim NumFiles As Integer, NumDirs As Integer
  100.     Screen.MousePointer = vbHourglass
  101.     List1.Clear
  102.     SearchPath = Text1.Text
  103.     FindStr = Text2.Text
  104.     FileSize = FindFilesAPI(SearchPath, FindStr, NumFiles, NumDirs)
  105.     Text3.Text = NumFiles & " Files found in " & NumDirs + 1 & " Directories"
  106.     Text4.Text = "Size of files found under " & SearchPath & " = " & Format(FileSize, "#,###,###,##0") & " Bytes"
  107.     Screen.MousePointer = vbDefault
  108. End Sub
  109.  
I think the above coding should work

Piyush
IT Engineer
<link removed>
Oct 12 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
7134
by: jajoo | last post by:
Hi everyone, I am trying to send files with multipart/form-date. Everything is ok with the send. But when I am receiving the files I should specify a directory where the files to be saved. The problem is that Tomcat saves the files into %CATALINA_HOME%/bin directory. I am wondering why? Why not into my application directory? The application directory is %CATALINA_HOME%/webapps/picture. As a parameter of directory I am giving "." – which...
0
2047
by: Webmaster | last post by:
Hi everyone... I know this isn't strictly a php/mysql ng, but I was hoping maybe someone here knows both languages and is willing to help. I have a directory of text files that i'd like to import into a msql table... I was wondering if there's an easier way to do this with php/mysql, I'd like to avoid copy/pasting the entire directory (thousands of files) into the database if i can.
0
2111
by: Don | last post by:
I intermittently get a runtime Compilation Error that says 'The compiler failed with error code 2000'. It appears that a DLL cannot be found in the 'temporary asp.net files' directory. The Detailed Compiler Output is at the bottom of this post. This is a custom dll named UtilitiesINGR.dll that I put in the GAC. I added a machine.config entry to instruct my app to use the GAC entry. Now I need to remove the dll from the GAC so I can...
1
1408
by: SevenThugsSoftware | last post by:
Does anyone know if there is a tool which can search a directory of ..dll files, for a particular namespace or object or method? -Nesim
4
3715
by: Stefan L | last post by:
Hi NG, I have a file driven application (a report server) which has to do some work when new files arrive or are deleted. When processing the notifications about newly created files from a FileSystemWatcher I want to open the file and read some info from it. Problem is that when opening the file directly when the event is fired I usually get a "file used by another process".
3
2928
by: Kimera.Kimera | last post by:
I'm trying to write a program in VB.net 2003 that basically deletes all files, folders, sub-folders and sub-sub folders (etc). The program is simply for deleting the Windows/Temp folder contents, removing all the files/folders inside it. The problem i am having is that i can only delete files in the Windows/Temp folder, and i can't delete folders if they contain files, i know that you can't do this.
1
1637
by: ahammad | last post by:
Hello, I would like to scan a certain directory for XML files. Then I want to take in every XML file, read it, and stores the contents as a string in an array. For example, if the directory has file1.xml, file2.xml, and file3.xml, they would all be scanned and read. Then, all the contents of file1.xml would be stored in an array as the first element: @array would contain the stuff in file1.xml @array would contain the stuff in file2.xml...
7
1850
by: shaiful | last post by:
Hi all, I have a problem that "How to open files from any particaular directory?" I mean just I wanna to press button then autometically will open all files from directory one by one, Thanks in Advance
7
27567
by: aboxylica | last post by:
i know how to open a file and access its elements..now i have to open a folder containing these files and work with these files.. the normal open("directoryname") is giving me an error. any suggestions?? looking for ur reply! cheers!
7
2098
by: paul86 | last post by:
Hi all, I have a vb question that i'm hoping I can get some help with. I have a directory of excel files (probably about 1000) of them. They are quote sheets for the company I work for, and they are all the exact same format. I need to create some code in vb using microsoft access that will scan through this directory of excel files, and pull certain cells of information from each of them, and store this information into an access database....
0
9687
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10237
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10029
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7567
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6808
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5467
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5588
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3761
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2941
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.