Tuesday, March 13, 2012

server.mappath

i want use a private collection of fonts. i want to tell the class file using the server.mappath method the location of the fonts folder.
but the class file throws an error when i used the server method there. is there any other ways to do it.

as of now i have avoided the problem by hardcoding it[by passing the physical location as a string]

But i would like to have another solution to my problem.

Try using HttpContext.Current.Server.MapPath

Thanks


That didn't work in the business logic. i added the system.web namespace and use the statement you gave me..but still it wont work

try

using

System.IO;

string

file =Path.Combine(Request.PhysicalApplicationPath,"/fonts/arial.ttf");

string fontlocation = HttpContext.Current.Server.MapPath("./Fonts") + fontName;

i used the above code,but it didnt' work


You cannot use . in the path.

string fontlocation = HttpContext.Current.Server.MapPath("/Fonts" + fontName);

Thanks


I just copied your code and tried to run the application, it threw my System.InvalidOperationException

My fonts folders resides in the application root folder. so how am i supposed to access it from the BLL[business logic layer]


Did you keep a breakpoint and check, where the exception was raised?

The above code is how you access the server.mappath in class file. (Make sure you are writing code in C# as the above code is in C#

Thanks


try

string fontsLocation = Request.ApplicationPath + "/Fonts";

Happy Coding


as i had said in my previous post, it threw, System.InvalidOperationException.

and yes i am writing them in C#.


Keep a breakpoint and check where it is throwing the xception.

Thanks


Yes it does, it did threw the exception i mentioned above sonow i have my coding in a try catch block.

can you post your complete code?

Thanks


PrivateFontCollection FontCollection =new PrivateFontCollection();try {string fontlocation =this.fontPath+"Fonts/"+this.FamilyName +".ttf";//string fontlocation = HttpContext.Current.Server.MapPath("./Fonts") + fontName; FontCollection.AddFontFile(fontlocation); FontFamily fontFamily = FontCollection.Families[0];this.imgFont =new Font(fontFamily,this.FontSize, style, GraphicsUnit.Pixel); }catch (System.IO.FileNotFoundException ex2) {this.familyName = System.Drawing.FontFamily.GenericSerif.Name;this.imgFont =new Font(this.FamilyName,this.FontSize, style, GraphicsUnit.Pixel); }

0 comments:

Post a Comment