Filename from Full Path?

  • Thread starter Thread starter Bruce Vander Werf
  • Start date Start date
B

Bruce Vander Werf

Is there a method anywhere for extracting just the filename part of a
full path name?

--Bruce
 
Hi Bruce,

That is as simple as using SubString method:
// not tested code.
string filename = fullpath.Substring( fullpath.LastIndexOf("\\")+1);

fullpath is a string containing the full path of the file, take a look at
the Path class.

Hope this help,
 
Like Peter mentioned, the Path class is an easy way to do
it....

System.IO.Path p;

string s = Crypto.EncryptFile(FileName
(i));
string shortName = p.GetFileName(s);

The filename property of the fileinfo class (I think) will
give it to you as well..but I know the top one does.

Cheers,

Bill
 
William Ryan said:
Like Peter mentioned, the Path class is an easy way to do
it....

System.IO.Path p;

string s = Crypto.EncryptFile(FileName
(i));
string shortName = p.GetFileName(s);

The filename property of the fileinfo class (I think) will
give it to you as well..but I know the top one does.

Path.GetFileName is a static method, and cannot be called with a
reference. This is good, as it means you don't end up with a variable
there for no good reason, and a call which looks like it's an instance
method.

(This is one area where C# wins over Java, although with suitable
settings in some IDEs such as Eclipse, you can turn it into a
warning/error in Java, too.)
 
Thanks, the Path class is exactly what I was looking for!

I am continually amazed at the richness of the FCL.

--Bruce
 
Back
Top