Thankyou I am using now : -
string path = OpenFile();
string file = Path.GetFileName(path);
lblFileName.Text = file;
which works a treat.
Can you tell me though. Is the path.GetFileName(path) just manipulating
the string in a similar fashion to what i first asked, but just
automatically? Or is it doing something fundamentally more different?
Thankyou very much,
Gary.
ktrvnbq02@sneakemail.com - 24 Nov 2006 11:31 GMT
> Can you tell me though. Is the path.GetFileName(path) just manipulating
> the string in a similar fashion to what i first asked, but just
> automatically? Or is it doing something fundamentally more different?
Essentially, probably yes. But the great thing about the Path class is
that it's all abstracted away from you, so have less worries about
compatibility with other platforms or future OSes, and there's less
chance for you to introduce an off-by-one bug or even a security
vulnerability caused by getting your app to parse malformed paths if
all the Path parsing is done centrally by the framework.
regards,
Matt
Marc Gravell - 24 Nov 2006 11:32 GMT
It is using standard methods, but the rules for how paths are formed are
complex - for instance, do you consider "/" and "\" to be equivalent? Have
you properly considered spaces / special characters in the path, etc.
The File class acts as a wrapper to this functionality, and is worth using.
In the more general caes there are lots of ways of working with strings; you
might have considered:
string a = @"abc\def\ghi"; // example path-esque string
string b = a.Substring(a.LastIndexOf('\\') + 1);
Marc
Arne Vajhøj - 24 Nov 2006 23:28 GMT
> string path = OpenFile();
> string file = Path.GetFileName(path);
[quoted text clipped - 5 lines]
> the string in a similar fashion to what i first asked, but just
> automatically? Or is it doing something fundamentally more different?
Probably the same.
(you can verify by decompiling Path.GetFileName)
The advantages are:
- your code are simpler
- your code are easier to read
- your code are safer because MS tests the framework and
it has a lot of users who will report any bugs
- your code are better prepared for the future since
if MS ever change the naming rules then they will also
update Path.GetFileName to work with the new format
Arne