views:

288

answers:

10

This seems to be a very odd problem that I cannot figure out for the life of me. I have a path (string) that looks like this:

D:\development\php\bchat\chat\index.php

I need to check if the file in question is a PHP file. I figure the most logical way is to take a substring starting from the . to the end of the string and see if it == .php

So i tried:

bool isphp = (path.Substring(path.LastIndexOf('.')) == ".php") ? true : false;

This always returned false. I thought maybe there was a trailing space at the end screwing me up so i put a TrimEnd() on path before it. But that didn't change anything. So i tried this:

bool isphp = (path.EndsWith(".php") == true) ? true : false;

This also always returns false.

EDIT I have now also tried this:

bool isphp = (Path.GetExtension(path) == ".php");

But this also returns false.

+8  A: 

Use the Path-class. It has a GetExtension() method:

var path =  @"D:\development\php\bchat\chat\index.php";
if( Path.GetExtension( path.ToUpperInvariant() ) == ".PHP" )
{}

EDIT: Added check for upper/lower cases

tanascius
This also seems to return false
The.Anti.9
The code I posted returns always true - please check if your string really contains what you posted. Do not forget to check for the dot (".php"). Debug or print out the extension that is evaluated by the GetExtension() method and check it.
tanascius
As pointed out by Martin - check for upper and lower case!
tanascius
The comparison should also be case insensitive as posted in my solution.
VVS
Yea I've been printing the path that i have, thats what I copied and pasted. I'm also printing the Path.GetExtension results and it says .php. but for some reason the evaluation returns false still.
The.Anti.9
Do you have whitespaces at the end of your path?
tanascius
I'm sure you've already checked, but print out the value of path followed by some other strings. E.g. Console.Writeline(path+"<<<") and ensure you don't have trailing characters (spaces) for some reason.
Martin Peck
+2  A: 

If it is an existing file you can get the FileInfo object for it and check the extension that way:

FileInfo fi = new FileInfo(@"D:\development\php\bchat\chat\index.php");
if (fi.Exists && fi.Extension == ".php")
{
    //Do something
}


Or I suppose you could be a sheep, follow the crowd and use the far better Path.GetExtension method that everyone else has suggested. But ask yourself this question first - do you want to do it the cleanest, fastest and best way, or do you want to assert your individuality and join me down the path of most resistance?

Martin Harris
+1  A: 

Use Path.GetExtension and compare with the file type you're looking for.

Brian Rasmussen
+1  A: 

What about Path.GetExtension method?

Kamarey
+1  A: 

UPDATE:

Check what path.Substring(path.LastIndexOf('.') actually returns - that might point you in the right direction.

ChrisF
Indeed, the solutions shown in the Q should work (albeit not the best way). Therefore path variable does not reference the expected value.
Richard
+1  A: 

Make sure that there is no difference in upper/lowercase. You are only testing for lowercase "php".

string ext = Path.GetExtension(path);
bool isPhp = (ext.Equals(".php", StringComparison.InvariantCultureIgnoreCase));
M4N
+2  A: 

Just a sample for the posted solution:

bool isphp = Path.GetExtension(path)
                 .Equals(".php", StringComparison.InvariantCultureIgnoreCase);
VVS
Still returns false.
The.Anti.9
Would be even better with StringComparison.OrdinalIgnoreCase.
Richard
+3  A: 

The following code works fine on my machine:

    public static void Main()
    {
        string path = @"D:\development\php\bchat\chat\index.php";
        bool isPhp = path.EndsWith(".php");
        Console.WriteLine(isPhp);
    }

So I would guess there is something else about your string that is causing it not to work. Maybe it is a case thing in which case add StringComparison.InvariantCultureIgnoreCase to your EndsWith call like this.

    public static void Main()
    {
        string path = @"D:\development\php\bchat\chat\index.pHp";
        bool isPhp = path.EndsWith(".php", StringComparison.OrdinalIgnoreCase);
        Console.WriteLine(isPhp);
    }

If that doesn't work put a break point on the comparison line and then type this into the Immediate window:

path[path.Length-1]

You should get this as a result:

112 'p'

If you don't you can tell that your path does not end with a standard p character.

Martin Brown
Better to use StringComparison.OrdinalIgnoreCase since culture (even invariant) is not appropriate with file names.
Richard
@Richard: Good point, I've updated the code to include this change.
Martin Brown
Thanks. For some reason Trim() wasn't removing the trailing space. so i had to hack around that
The.Anti.9
What was the character code of the character? I'm going to guess it was 0 (a null terminator).
Martin Brown
+1  A: 
private static bool IsPhp(string fileName)
{
    return string.Compare(Path.GetExtension(fileName), ".php",
     StringComparison.OrdinalIgnoreCase) == 0;
}
odalet
+1  A: 

I suspect there's something "odd" in your string. I suggest you dump your string out in detail, like this:

foreach (char c in path)
{
    Console.WriteLine("'{0}' U+{1:x4}", c, (int) c);
}

That way you'll see any unexpected characters, e.g. unicode char 0s between "real" characters.

Jon Skeet