Get filenames

N

Niels

Hello, i want to read out the file names of a directory.
The previous time, i got this piece of code from someone,
but this code counts the number of files in a directory.

My intentions are to get the filenames (without extension)
in a combobox....
~~~~~~~~~~
Code
~~~~~~~~~~
Imports System
Imports System.IO



Dim di As DirectoryInfo = New DirectoryInfo("c:\")
Try
Dim dirs As FileSystemInfo() = di.GetDirectories("*p*")
Console.WriteLine("Number of directories with a p: _
{0}", dirs.Length)
Dim diNext As DirectoryInfo
For Each diNext In dirs
Console.WriteLine("The number of files and _
directories " + "in {0} with an e is {1}", diNext, _
diNext.GetFileSystemInfos("*e*").Length)
Next
Catch e As Exception
Console.WriteLine("The process failed: {0}", _
e.ToString())
End Try
~~~~~~~~~~
Code
~~~~~~~~~~

Can anyone help me???
 
A

Armin Zingler

Niels said:
Hello, i want to read out the file names of a directory.
The previous time, i got this piece of code from someone,
but this code counts the number of files in a directory.

My intentions are to get the filenames (without extension)
in a combobox....
~~~~~~~~~~
Code
~~~~~~~~~~
Imports System
Imports System.IO



Dim di As DirectoryInfo = New DirectoryInfo("c:\")


Call di.GetFiles instead of di.GetDirectories. In a loop, call
System.IO.Path.GetFileNameWithoutExtension and add the return value to the
combobox. Pass the Name property of each FileInfo object returned by
di.GetFiles to GetFileNameWithoutExtension.


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
T

Thom

Here is the way I do it....
fileNameArray = System.IO.Directory.GetFiles("C:\", "*.")
 
P

parth_mca

dim filenames() as string
filenames = Directory.getFiles("path u want to get files from","filter
u like")

this will give u the filenames array filled with the filenames but
each filename will b a full path not just name
like c:\aaa\bbb\ccc\zzz.txt instead of just zzz.tzt
so u'll have to travers all filename array and for each file name u'll
have to split it n get the last element as filename
as
dim justname(),longname() as string

for i=0 to filenames.length-1
longname = filenames(i).split("\")
justname(i) = longname(array.length-1) ' the last element of splitted
string which is the filename
next
'' now this justname() is the array that contains filenames u want

good luck....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top