< Split File from File+Path >

C

Carsten Kraft

Hello Newsgroup,

I am searching for function which returns me filename from a string that has
the file + filepath.
( string sFile = "C:\DIR\SUBDIR\TEST.XML" => TEST.XML)

Is there a functional class for this or what have I to do?

Thanks alot ...
 
D

Daniel Bass

there's a few ways you can do it...

I like to take advantage of the split method in the string object.

string getFileName ( string szPath )
{
string [] szTokens = szPath.Split('\\');
return szTokens[ szTokens.Length - 1 ];
}

To use, you'd say:

string szFileName = getFileName ( szPath );

hope that helps.
Dan.

Hello Newsgroup,

I am searching for function which returns me filename from a string that has
the file + filepath.
( string sFile = "C:\DIR\SUBDIR\TEST.XML" => TEST.XML)

Is there a functional class for this or what have I to do?

Thanks alot ...
 
D

Dmitriy Lapshin [C# / .NET MVP]

Hi Carsten,

Take a look at the GetFileName method on the System.IO.Path class.
 
S

Steve Barnett

Try:

string sFile = "C:\\DIR\\SUBDIR\\TEST.XML";
sFile = sFile.Substring(sFile.LastIndexOf("\\")+1);

Steve

| Hello Newsgroup,
|
| I am searching for function which returns me filename from a string that
has
| the file + filepath.
| ( string sFile = "C:\DIR\SUBDIR\TEST.XML" => TEST.XML)
|
| Is there a functional class for this or what have I to do?
|
| Thanks alot ...
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top