Wednesday 18 April 2012

Beginning .Net : Working with Path Class using C# Programming

.NET Framework provides you with a class for working with file paths. The System
.IO.Path class has many static methods to work with paths.
Some static methods are
ChangeExtension()
Combine()
GetDirectoryName()
GetExtension()
GetFileName()
GetFileNameWithoutExtension()
GetFullPath()
GetInvalidFileNameChars()
GetInvalidPathChars()
GetPathRoot()
GetTempFileName()
GetTempPath()
HasExtension()
IsPathRooted()

Here are sample example for this.
        string strFilePath = "D:\\Others\\bookmarks.html";
        Response.Write(Path.GetPathRoot(strFilePath));
        Response.Write("</br>");
        Response.Write(Path.GetDirectoryName(strFilePath));
        Response.Write("</br>");
        Response.Write(Path.GetFileName(strFilePath));
        Response.Write("</br>");
        Response.Write(Path.GetFileNameWithoutExtension(strFilePath));
        Response.Write("</br>");
        Response.Write(Path.GetExtension(strFilePath));
        Response.Write("</br>");
        Response.Write(Path.GetTempPath());
        Response.Write("</br>");
        Response.Write(Path.DirectorySeparatorChar.ToString());
        Response.Write("</br>");
        Response.Write(Path.AltDirectorySeparatorChar.ToString());
        Response.Write("</br>");
        Response.Write(Path.VolumeSeparatorChar.ToString());
        Response.Write("</br>");
        Response.Write(Path.PathSeparator.ToString());
        Response.Write("</br>");
        Response.Write(HttpUtility.HtmlEncode(new String(Path.GetInvalidPathChars())));
        Response.Write("</br>");
        Response.Write(HttpUtility.HtmlEncode(new String(Path.GetInvalidFileNameChars())));
        Response.Write("</br>");

1 comment: