Solved

Get filename out of a string

Posted on 2006-11-03
4
208 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

895 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now