Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Get filename out of a string

Posted on 2006-11-03
4
Medium Priority
?
216 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 2000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

715 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