Friday, December 28, 2012

How to get relative file path from full path

Let say you have a Folder called "BookCovers" within your application folder. To get the full path of a file inside the folder use the below code.

string FullPath = Server.MapPath(@"~/BookCovers/") + FileName;

EX: E:\Industry\Soft\Tests\\BookCovers\1.jpg

To get the relative path from full path use the below code.

String RelativePath = FullPath.Replace(Request.ServerVariables["APPL_PHYSICAL_PATH"], String.Empty);
RelativePath = @"~\" + RelativePath;

EX: ~\BookCovers\1.jpg

No comments:
Write comments
Recommended Posts × +