Solved

Get filename out of a string

Posted on 2006-11-03
4
210 Views
Last Modified: 2010-04-16
I have a the following string:

C:\Documents and Settings\test\My Documents\My Pictures\Logo2.gif

How do I just get the

Logo2.gif

out of the string

I'm using

strFileNamePath.Substring(strFileNamePath.LastIndexOf("\\"), strFileNamePath.Length);

But I get an error all the time

"Index and length must refer to a location within the string. Parameter name: length "

Many thanks
0
Comment
Question by:mousemat24
  • 2
4 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 17869252
Use:

            string fullpath = @"C:\Documents and Settings\test\My Documents\My Pictures\Logo2.gif";
            string filename = System.IO.Path.GetFileName(fullpath);
0
 

Author Comment

by:mousemat24
ID: 17869303
Thanks carl_tawn !! Quick quetion why dosnt my ver work?

strFileNamePath.Substring(strFileNamePath.LastIndexOf("\\"), strFileNamePath.Length);

I cant see why that should cause an error?
0
 
LVL 13

Expert Comment

by:joechina
ID: 17869333
Try

strFileNamePath.Substring(strFileNamePath.LastIndexOf("\\") + 1);


Or Try the following two ways

1)
result = (new FileInfo(strFileNamePath)).Name;

2)
result = Regex.Match(strFileNamePath, "[^\\\\]+$").Value;

0
 
LVL 13

Expert Comment

by:joechina
ID: 17869350
strFileNamePath.Length is the length of both filename and path.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Video Player 12 27
Hey Audio! 9 44
How to read text with RegEx... 2 25
C# MVC Insert Multiple Row into DB 2 30
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question