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

Extract file name

P: n/a
I got a string. eg.

C:\Temp\Documents\MyDoc.doc

How do I get the name of the document only?
ie. MyDoc.doc
Jul 27 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a


Alan T wrote:
I got a string. eg.

C:\Temp\Documents\MyDoc.doc

How do I get the name of the document only?
ie. MyDoc.doc

Try this:

using System.IO;

FileInfo fileInfo = new FileInfo(fileName);
string nameWithoutPath=fileInfo.Name.
Jul 27 '06 #2

P: n/a
Jianwei,

That's a bit much, actually. I would recommend using the static
GetFileName method on the Path class in the System.IO namespace.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jianwei Sun" <js***********@gmail.comwrote in message
news:eE**************@TK2MSFTNGP05.phx.gbl...
>

Alan T wrote:
>I got a string. eg.

C:\Temp\Documents\MyDoc.doc

How do I get the name of the document only?
ie. MyDoc.doc

Try this:

using System.IO;

FileInfo fileInfo = new FileInfo(fileName);
string nameWithoutPath=fileInfo.Name.


Jul 27 '06 #3

P: n/a
Thanks, it works.

After I got the name, how do I get rid of the extension ?

Eg.
MyDoc.doc to MyDoc

"Jianwei Sun" <js***********@gmail.comwrote in message
news:eE**************@TK2MSFTNGP05.phx.gbl...
>

Alan T wrote:
>I got a string. eg.

C:\Temp\Documents\MyDoc.doc

How do I get the name of the document only?
ie. MyDoc.doc

Try this:

using System.IO;

FileInfo fileInfo = new FileInfo(fileName);
string nameWithoutPath=fileInfo.Name.


Jul 27 '06 #4

P: n/a
Alan,

You can pass the file name to the static GetFileNameWithoutExtension
method on the Path class.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Alan T" <al*************@yahoo.com.auwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
Thanks, it works.

After I got the name, how do I get rid of the extension ?

Eg.
MyDoc.doc to MyDoc

"Jianwei Sun" <js***********@gmail.comwrote in message
news:eE**************@TK2MSFTNGP05.phx.gbl...
>>

Alan T wrote:
>>I got a string. eg.

C:\Temp\Documents\MyDoc.doc

How do I get the name of the document only?
ie. MyDoc.doc

Try this:

using System.IO;

FileInfo fileInfo = new FileInfo(fileName);
string nameWithoutPath=fileInfo.Name.



Jul 27 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.