?
Solved

Changing System.Path.GetFileName(string path)

Posted on 2010-11-26
6
Medium Priority
?
513 Views
Last Modified: 2013-12-17
In my winform app I use the System.Path.GetFileName(string path).
I would like that if the string is a web address  (like : "http://www.123.com/1.htm") it will return the string without any change.
How can I do this ?
0
Comment
Question by:yaron89
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 5

Assisted Solution

by:buraksarica
buraksarica earned 500 total points
ID: 34215944
You can check against this situation like :

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

Open in new window

0
 
LVL 21

Assisted Solution

by:Alpesh Patel
Alpesh Patel earned 500 total points
ID: 34216420
Hi,

You can get page name from Server Variables of Request object.
0
 

Accepted Solution

by:
yaron89 earned 0 total points
ID: 34216422
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.
0
Application Discovery Service in AWS

In the era of the cloud, customers migrating away from their existing on-premise infrastructure. This requires lots of planning, strategies, and effort to identify their existing resources and determine how best to migrate.  Datacenter migrations happen in four phases -

 
LVL 13

Assisted Solution

by:Naman Goel
Naman Goel earned 500 total points
ID: 34216897
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)
    {
        CheckInvalidPathChars(path);
        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

0
 
LVL 20

Assisted Solution

by:BuggyCoder
BuggyCoder earned 500 total points
ID: 34216902
if you write Path.GetFileName(@"http:\\www.123k.com\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:\\www.kochar.com\123.htm";
string result = (Path.GetFileName(path + @"\").Length == 0 ? path : Path.GetFileName(path));

Console.WriteLine("Path is: " + result);
0
 

Author Closing Comment

by:yaron89
ID: 34399048
.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question