C# .NET - server.mappath alternative in C#

Asked By asif hameed on 12-Nov-10 05:52 AM
Hi Tailor:

Thanks for quick response. I am getting error on Server.mappath. What is alternative to this in windows application. My reports are on root of windows application.

Regards,
Asif Hameed
Nowshad M replied to asif hameed on 12-Nov-10 05:56 AM
Hi,
May i know whats the error u r getting?
Parag Satpute replied to asif hameed on 12-Nov-10 05:56 AM
The problem is, you've used a physical path, and not a virtual one,
Either remove Server.MapPath(), or change the path to a virtual one.....
Jatin Prajapati replied to asif hameed on 12-Nov-10 06:09 AM
See asif, Server.MapPath is only for ASP.NET. you Can use it in Windows applications. But, as you are saying that your reports are on root of windows application, the while executing you application from Visual Studio, you have to provide a path like given in below code example.

FileStream fs = File.Open("../../MyReport.log",FileMode.Open,FileAccess.Read);

If you have execution your application after installation, then you have to remove the "../../" from the above path.

The purpose of "../../" is that your application's exe file is in bin/debug folder, and to refer the files at the root of the application, you have to track back those two directories. While in the installed application you don't need to do that.

Try this and feel free to write if you have any queries.
Santhosh N replied to asif hameed on 12-Nov-10 06:10 AM
Path.GetDirectoryName(Application.ExecutablePath); will give you the root path of the application in windows..
Reena Jain replied to asif hameed on 12-Nov-10 06:12 AM
hi asif,

in windows application just keep your report in debug folder to get this using following code

AppDomain.CurrentDomain.BaseDirectory+ "reportname";

base directory in windows application work as a server.mappath, but base directory target to base folder of application.

hope this will help you
Mitesh Darji replied to asif hameed on 12-Nov-10 06:16 AM
you can use Application.StartupPath in Windows Application
asif hameed replied to Mitesh Darji on 12-Nov-10 06:37 AM
Hi all:

Thanks for your response.  I am giving following path for my report.

report.FileName = Application.StartupPath + "/CrystalReport4.rpt";

and when i put it in watch, the value is:

rassdk://C:\Documents and Settings\nadeempc\Desktop\CRTesting\CRTesting\bin\DebugCrystalReport4.rpt

I used all above suggestions and get same path. Please suggest me solution to this.

Regards,
Asif Hameed
Rohan Dave replied to asif hameed on 12-Nov-10 06:46 AM
In windwos base application, you can't use Server.MapPath( ). This mehtod is only for the asp.net application which will map the relative or virtual path to the physical path to the corresponding directory on your server.

Now in the Windows base application, by default application use the path for executable file that started the application. So it will be always "bin\Debug" or "bin\Release" based on your build configuration of application. So that you must need to come out of this two directory as your file is inside the root application folder. So use "../../yourfilename" to access the File.

FileStream objFs = new FileStream("../../Copy of b.csv", FileMode.Open, FileAccess.Read);

hope it will help.

asif hameed replied to Rohan Dave on 12-Nov-10 06:54 AM
Hi Rohan:

Thanks for the suggestion. 

I used below code:

 report.FileName = "./../" + Application.StartupPath + "/CrystalReport4.rpt"; 

and checked in wtch. Value is:

rassdk://./../C:\Documents and Settings\nadeempc\Desktop\CRTesting\CRTesting\bin\DebugCrystalReport4.rpt

Regards,
Asif Hameed
Rohan Dave replied to asif hameed on 12-Nov-10 07:22 AM
can you please let me know which type of "report" variable is ? i mean int, string or something like that...
undhad ashwin replied to asif hameed on 15-Nov-10 05:10 PM

hi

You can also try System.IO.FileSystemInfo and System.IO.DirectoryInfo Class to retrieve the path

System.IO.FileInfo mobjFileInfo;
mobjFileInfo = new FileInfo("filepath");
mobjFileInfo.DirectoryName - Gets a string representing the directory's full path.

mo mo replied to Rohan Dave on 31-May-12 01:37 AM
HI

asif hameed

i tried below code:

 string savepath =@"..\..\images\"+imgname;
System.Drawing.Image image = System.Drawing.Image.FromFile(savepath);

It is ok for me. You should try this way.

Regards
momo