By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,107 Members | 1,454 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,107 IT Pros & Developers. It's quick & easy.

Trimming a string for a filename

P: 38
Situation : Need to get a list of files in a directory, but I am currently getting the whole path of the file (Example: "C:\A Folder\Another Folder\file.txt"). What I want is just the filename and extension. So from above example, all I actually want is "file.txt".

Language: VB.NET 2005

Current Code:

Expand|Select|Wrap|Line Numbers
  1. Imports System.Collections.ObjectModel
  2. ...
  3. Dim files As ReadOnlyCollection(Of String)
  4. files = My.Computer.FileSystem.GetFiles(strCurDir)
  5.  
Obviously, the GetFiles function returns a ReadOnlyCollection(Of String), so I cannot simply change it to below...

Expand|Select|Wrap|Line Numbers
  1. Imports System.Collections.ObjectModel
  2. ...
  3. Dim files As ReadOnlyCollection(Of File)
  4. files = My.Computer.FileSystem.GetFiles(strCurDir)
  5.  
With just a string of something like "C:\Folder\Folder\Folder\somefile.txt", what would be the easiest method of getting everything after the last "\"?
Jul 12 '06 #1
Share this Question
Share on Google+
3 Replies


P: 2
intStartPos=strFullFileName.LastIndexOf("\\")+1;
strFileName=strFullFileName.Substring(intStartPos, strFullFileName.Length-intStartPos);

the above code would do. Remove the ; if you are using VB.NET
Jul 13 '06 #2

P: 38
Ah ok, Thank you
Jul 14 '06 #3

P: 3
There's an easier way: System.IO.Path.GetFileName(strFilePath).
Jul 14 '06 #4

Post your reply

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