Changing System.Path.GetFileName(string path)

In my winform app I use the System.Path.GetFileName(string path).
I would like that if the string is a web address  (like : "") it will return the string without any change.
How can I do this ?
Who is Participating?
yaron89Connect With a Mentor Author Commented:
Thanks. My ask is about changing the System.Path.GetFileName(string path) so I won't have to check against  any situation in the code.
buraksaricaConnect With a Mentor Commented:
You can check against this situation like :

result = mypath;
if (false==mypath.StartsWith("http://"))
     result = System.Path.GetFileName(mypath);

Open in new window

Alpesh PatelConnect With a Mentor Assistant ConsultantCommented:

You can get page name from Server Variables of Request object.
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Naman GoelConnect With a Mentor Software engineer 1Commented:
No, Path.GetFileName() is not doing anything special other than simple string extraction

Please look at below attached code for complete internal implementation of Path.GetFileName(path) method

you can instead use any other method for your purpose as suggested by buraksarica,PatelAlpesh or you can create your own URL validation method
 like below and use this method to check for URL and return the same URL for this method returns true otherwise extract filename by Path.GetFileName() method.

/// <summary>
/// method for validating a url with regular expressions
/// </summary>
/// <param name="url">url we're validating</param>
/// <returns>true if valid, otherwise false</returns>
public static bool isValidUrl(ref string url)
    string pattern = @"^(http|https|ftp)\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?/?([a-zA-Z0-9\-\._\?\,\'/\\\+&amp;%\$#\=~])*[^\.\,\)\(\s]$";
    Regex reg = new Regex(pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
    return reg.IsMatch(url);
public static string GetFileName(string path)
    if (path != null)
        int length = path.Length;
        int num2 = length;
        while (--num2 >= 0)
            char ch = path[num2];
            if (((ch == DirectorySeparatorChar) || (ch == AltDirectorySeparatorChar)) || (ch == VolumeSeparatorChar))
                return path.Substring(num2 + 1, (length - num2) - 1);
    return path;

Open in new window

BuggyCoderConnect With a Mentor Commented:
if you write Path.GetFileName(@"http:\\\123.htm"); it will return 123.htm however if you append '\' at the end it will return nothing. so you can check if nothing is returned you can take the passed variable as output in the following way:-

string path = @"http:\\\123.htm";
string result = (Path.GetFileName(path + @"\").Length == 0 ? path : Path.GetFileName(path));

Console.WriteLine("Path is: " + result);
yaron89Author Commented:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.