Program that tests Path class [C#]

Posted by senthil | 7:56:00 PM | , | 0 comments »

The extension of the file, the actual filename, the filename without extension, and path root. The path root will always be "C:\\", with the trailing separator, even when the file is nested in many folders. GetFileName. You can get the filename alone by calling the Path.GetFileName method. This will return the filename at the end of the path, along with the extension, such as .doc or .exe.

extension —Path.GetFileNameWithoutExtension.

It is useful to see the results of the Path methods on various inputs. Sometimes the methods handle invalid characters as you might expect. Sometimes they do not. This program calls three Path methods on an array of possible inputs.
using System;
using System.IO;

class Program
{
    static void Main()
    {
 string[] pages = new string[]
 {
     "cat.aspx",
     "really-long-page.aspx",
     "test.aspx",
     "invalid-page",
     "something-else.aspx",
     "Content/Rat.aspx",
     "http://dotnetperls.com/Cat/Mouse.aspx",
     "C:\\Windows\\File.txt",
     "C:\\Word-2007.docx"
 };
 foreach (string page in pages)
 {
     string name = Path.GetFileName(page);
     string nameKey = Path.GetFileNameWithoutExtension(page);
     string directory = Path.GetDirectoryName(page);
     //
     // Display the Path strings we extracted.
     //
     Console.WriteLine("{0}, {1}, {2}, {3}",
  page, name, nameKey, directory);
 }
    }
}
OutPut

Input:                       cat.aspx
GetFileName:                 cat.aspx
GetFileNameWithoutExtension: cat
GetDirectoryName:            -

Input:                       really-long-page.aspx
GetFileName:                 really-long-page.aspx
GetFileNameWithoutExtension: really-long-page
GetDirectoryName:            -

Input:                       test.aspx
GetFileName:                 test.aspx
GetFileNameWithoutExtension: test
GetDirectoryName:            -

Input:                       invalid-page
GetFileName:                 invalid-page
GetFileNameWithoutExtension: invalid-page
GetDirectoryName:            -

Input:                       Content/Rat.aspx
GetFileName:                 Rat.aspx
GetFileNameWithoutExtension: Rat
GetDirectoryName:            Content

Input:                       http://dotnetperls.com/Cat/Mouse.aspx
GetFileName:                 Mouse.aspx
GetFileNameWithoutExtension: Mouse
GetDirectoryName:            http:\dotnetperls.com\Cat

Input:                       C:\Windows\File.txt
GetFileName:                 File.txt
GetFileNameWithoutExtension: File
GetDirectoryName:            C:\Windows

Input:                       C:\Word-2007.docx
GetFileName:                 Word-2007.docx
GetFileNameWithoutExtension: Word-2007
GetDirectoryName:            C:\

0 comments

Related Posts Plugin for WordPress, Blogger...