< Split File from File+Path >

  • Thread starter Thread starter Carsten Kraft
  • Start date Start date
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 ...
 
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 ...
 
Hi Carsten,

Take a look at the GetFileName method on the System.IO.Path class.
 
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 ...
|
|
 
Back
Top