Extract file name

  • Thread starter Thread starter Alan T
  • Start date Start date
A

Alan T

I got a string. eg.

C:\Temp\Documents\MyDoc.doc

How do I get the name of the document only?
ie. MyDoc.doc
 
Alan said:
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.
 
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.
 
Thanks, it works.

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

Eg.
MyDoc.doc to MyDoc
 
Alan,

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

Hope this helps.
 

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

Similar Threads

Start function 15
Cant open section of Word Document 1
Detect file type 7
Hyperlink - relative 2
What is this convention named? 8
Just File Name 6
Open files in associated editors. 1
Word Document 2

Back
Top