By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,060 Members | 1,878 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,060 IT Pros & Developers. It's quick & easy.

Extract file name from path

P: n/a
It's probably very simple... I would like to know if there is a function
similar to ExtractFileName() (I've used it in Delphi), which returns only
the file name from the full path, e.g. "C:\Temp\MyFile.txt" would return
"MyFile.txt".

Thanks!

- Kristoffer -
Nov 15 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
L#
On Fri, 6 Feb 2004 13:47:57 +0100, "Kristoffer Persson" <hidden>
wrote:
It's probably very simple... I would like to know if there is a function
similar to ExtractFileName() (I've used it in Delphi), which returns only
the file name from the full path, e.g. "C:\Temp\MyFile.txt" would return
"MyFile.txt".

Thanks!

- Kristoffer -


System.IO.Path.GetFileName(string path);
--
Ludwig
mailto:ludwig_(nospamplease)stuyck@pandora(nospamp lease).be
Nov 15 '05 #2

P: n/a
hm, I found no function that performs this. so I suppose this will help
you...

string GetFileName(string szPath)

{

string szRetVal = string.Empty;

string szTmp = string.Empty;

int j = szPath.Length - 1;

while (szPath[j] != '\\')

{

szTmp += szPath[j];

j--;

}
// you have the reversed file name

// replace characters...
int i = szTmp.Length;

while (i != 0)

{

i--;

szRetVal += szTmp[i];

}

szTmp = null;

return szRetVal;

}
--
/*
Vladimir Scherbina,
Ukraine, Kiev.
*/

"Kristoffer Persson" <hidden> wrote in message
news:#e**************@TK2MSFTNGP10.phx.gbl...
It's probably very simple... I would like to know if there is a function
similar to ExtractFileName() (I've used it in Delphi), which returns only
the file name from the full path, e.g. "C:\Temp\MyFile.txt" would return
"MyFile.txt".

Thanks!

- Kristoffer -

Nov 15 '05 #3

P: n/a

Hi, Vladimir Scherbina!
hm, I found no function that performs this. so I suppose this will help
you...

string GetFileName(string szPath)

{

string szRetVal = string.Empty;

string szTmp = string.Empty;

int j = szPath.Length - 1;

while (szPath[j] != '\\')

{


I optimize your code ;-)

string GetFileName(string szPath)
{
string[] s_arr = szPath.Split(new char[]{'\\'});
return s_arr[s_arr.Length-1];
}

or

string GetFileName(string szPath)
{
Regex r = new Regex(@"\w+[.]\w+$+");
return r.Match(szPath).Value;
}
--
Roman S. Golubin
ICQ UIN 63253392
go*****************@arhcity.ru
Nov 15 '05 #4

P: n/a

Hi, Kristoffer Persson!
It's probably very simple... I would like to know if there is a function
similar to ExtractFileName() (I've used it in Delphi), which returns only
the file name from the full path, e.g. "C:\Temp\MyFile.txt" would return
"MyFile.txt".


string ExtractFileName(string szPath)
{
Regex r = new Regex(@"\w+[.]\w+$+");
return r.Match(szPath).Value;
}
--
Roman S. Golubin
ICQ UIN 63253392
go*****************@arhcity.ru
Nov 15 '05 #5

P: n/a
The static methods of Path give you a myriad of options to deal with file
names that include the path.

For example:

string myFile = "C:\Temp\MyFile.txt";
string filename = Path.GetFileName(myFile);

You could also get the name without extension:

filename = Path.GetFileNameWithoutExtension(myFile);

Just browse through all the different methods that Path gives you to see
what else you can do.

- Robert Sentgerath -
Path.GetFileNameWithoutExtension
"Kristoffer Persson" <hidden> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
It's probably very simple... I would like to know if there is a function
similar to ExtractFileName() (I've used it in Delphi), which returns only
the file name from the full path, e.g. "C:\Temp\MyFile.txt" would return
"MyFile.txt".

Thanks!

- Kristoffer -

Nov 15 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.