Solved

Get filename out of a string

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Aspnet xml 2 36
import issue in bit values 3 44
ConsoleSql 1 18
c# if statement weird reaction 3 29
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
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.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 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

24 Experts available now in Live!

Get 1:1 Help Now