Get File Name : File Name « File Stream « C# / C Sharp






Get File Name

     
//
// Pauthor - An authoring library for Pivot collections
// http://pauthor.codeplex.com
//
// This source code is released under the Microsoft Code Sharing License.
// For full details, see: http://pauthor.codeplex.com/license
//


using System;
using System.IO;
using System.Net;

public static class UriUtility
{


    public static String GetFileName(String path)
    {
        if (IsLocalFile(path))
        {
            return Path.GetFileName(path);
        }
        else
        {
            Uri uri = new Uri(UriUtility.ExpandUri(path), UriKind.RelativeOrAbsolute);
            if (uri.IsAbsoluteUri == false) return Path.GetFileName(path);
            if (uri.Segments.Length <= 1) return null;

            return uri.Segments[uri.Segments.Length - 1];
        }
    }
    public static bool IsLocalFile(String path)
    {
        Uri uri = new Uri(path, UriKind.RelativeOrAbsolute);
        if (uri.IsAbsoluteUri) return uri.IsFile;
        if (File.Exists(path) || Directory.Exists(path)) return true;
        return false;
    }
    public static String ExpandUri(String path)
    {
        Uri uri = new Uri(path, UriKind.RelativeOrAbsolute);

        if (uri.IsAbsoluteUri)
        {
            if (uri.IsFile) return uri.LocalPath;
            return uri.AbsoluteUri;
        }

        if (File.Exists(path) || Directory.Exists(path)) return Path.GetFullPath(path);

        return path;
    }

}

   
    
    
    
    
  








Related examples in the same category

1.Query file name with Linq
2.Clean File Name
3.Retrieves the date from the beginning of the WinSat filename and creates a datetime object from it
4.Convert To Legal File Name
5.Get File Name From Path
6.Extracts a path part of a full path filename, searching for the last directory separator or column.
7.Creates a relative path from absolute pathfilename and a reference path.
8.Get file extension
9.Restrict a list of files to valid extensions only
10.Get Content Type by Extension
11.Get Full Path Without Extension
12.Full Name Without Extension
13.Name Without Extension
14.ImageFormat to Extension
15.Gets the extension without the dot
16.Get Image Type Extension