tags:

views:

132

answers:

3

Hello, I have a string in C#

String file="\\mserver-80\docs\somedoc.doc"

Now How do I get fileInfo from the above sting. What I mean is, I want to declare something like

FileInfo fInfo = new FileInfo(file);
fileExtn = fInfo.Extension;
+4  A: 

That code will work fine, using the FileInfo class.

Simply add

using System.IO;

However, note that the \ must be escaped as \\.
Instead, you should use an @"" string, like this:

String file = @"\\mserver-80\docs\somedoc.doc"
SLaks
+6  A: 

In C# the string should be

String file="\\\\mserver-80\\docs\\somedoc.doc";

You can also escacpe the string using the @ character, which is a better alternative:

String file=@"\\mserver-80\docs\somedoc.doc";

other than that the code should work.

Waleed Al-Balooshi
Good point. `@"\\mserver-80\docs\somedoc.doc"` will also work. However `\d` and `\s` will not compile, so it is easy to catch that error.
Kobi
Good point Kobi, using @ is a better alternative. I have updated the answer.
Waleed Al-Balooshi
+13  A: 

You can also try

Path.GetExtension(file)
josephj1989
This is a better solution than instatiating a FileInfo object if all you need is the extension. If you'll be doing more than that, keep with the FileInfo, otherwise use this (I also believe the File static class has similar functionality, but I couldn't swear to that..._
AllenG
Many dont know there exists a "Path" class,the simply and beauty...
Srinivas Reddy Thatiparthy