Need method to get file names

aewarnick

Well-known member
Joined
Jan 29, 2003
Messages
1,031
I need a method that will enable me to get only the file names in a directory and put them as strings into a string array. Does anyone know something like that or close to it?
 
Use System.IO.Path.GetFilename(strFullPath) to get just the filename from a fully-qualified path.
 
string directory=house+"\\"+"CommLog"+date+"RTB"+(i+1);
FileArr[0]=Path.GetFileNameWithoutExtension(directory);

It only returns the name I already know(RTB1,RTB2,RTB3...). It does not return the names of the files in the folder.

Another topic:

How can I say "Get anything"? Ex:
"c:\\folder\\"Get anything"\\file".
 
When you have the fully qualified path plus filename in a string you can split the string with "\" as the splitter and the select the last element.

pseudocode ahead
Code:
Dim longName as string
Dim shortName as string

Dim parts() as string

parts = split(longName, "\")
shortName = parts(UBound(parts))
 
That is VB not C#. And I need something that will not return any file name in any folder I specify no matter what the folder is.

I think that can be done with Batch files like this:
c:\folder\..\folder\..
Actually, that probably wont work, but you should get my point.
 
You want a function that doesnt return files? I really dont
understand what youre looking for. Perhaps you should tell us what
the program is that you are trying to make?
 
The program saves the documents as the names of the labels. That is why I need the names of the files and only the files (no extention) because I will use it to name the labels when the files are opened.
 
OK, so use the GetFiles method (as shown above) to get the files
into an array (this will include extensions). Then when you do need
the filename without extension:
Code:
Dim noExt, dummy As String

For Each dummy In TheListOfFilesArray
  noExt = dummy.Split(".")(0)
  noExt contains the file w/o extension so do something with it here
Next
 
Had to jump though some hoops for this one!

FileArr=Directory.GetFiles(directory);

MessageBox.Show(FileArr);
string[]file=FileArr.Split(.);
int start=file.LastIndexOf("\\");
file=file.Substring(start+1);
MessageBox.Show(file);

Does anyone know the answer to the question posted in my other post above, about how I can say "any folder or file" in my code?
 
Here is what I wrote:

And I need something that will not return any file name in any folder I specify no matter what the folder is.

I think that can be done with Batch files like this:
c:\folder\..\folder\..
Actually, that probably wont work, but you should get my point.
 
Back
Top