views:

833

answers:

3

Hello guys first of all apologize for asking such a simple and yet redundant question but it seems my case is kinda bit different where googling failed to provide answers. I have a solution with 2 projects say proj1 and proj2 where proj1 is a winform application and proj2 is a classlibrary application.proj1 is sitting here

C:\Documents and Settings\myname\My Documents\Visual Studio 2005\Projects\proj1\proj1

ane the classlibrary is here

C:\Documents and Settings\myname\My Documents\Visual Studio 2005\Projects\proj1\proj2

Now i have a report in a subfolder Report where i have my crystalreport and other stuffs i need to have this root path of proj1

C:\Documents and Settings\myname\My Documents\Visual Studio 2005\Projects\proj1\proj1

so that i append @"Report\myreport.rpt" to it. I believe with that it's supposed to be relative path on the deployed machine.correct me if I'm wrong.the main idea is to have relative path to the Report folder at

C:\Documents and Settings\myname\My Documents\Visual Studio 2005\Projects\proj1\proj\Report.

from all i searched online like Environment.CurrentDirectoryor Application.StartupPath or Application.ExecutablePath or System.Reflection.Assembly.GetExecutingAssembly().Location are just given me either the bin/proj1.exe or the debug folder. i just can't figure out how to do that

thank you for reading this.oh yeah! and for your kind answer. PS: using C#2.0

A: 

I think the safest way to find in which folder your assembly is located is this:

private static string GetAppFolder()
{
    return new FileInfo(Assembly.GetExecutingAssembly().Location).Directory.FullName;
}

...and using that function you can get your report path:

Path.Combine(GetAppFolder(), @"Report\myreport.rpt")
Fredrik Mörk
Thanks for the fast reply.I output you suggestion with a messagebox and i 'm having this ...\bin\debug\Report\myreport.rpt.I guess it won't work on my case
black sensei
If you are running your application from within Visual Studio, and if you are running a debug build, it will be running from the bin\debug directory. If you move your .exe file (and the Report directory) to some other directory, GetAppPath() will return that directory instead. The "bin\debug" part is not compiled into the program. I assume that the "Report" is a sub directory to the directory where your .exe file is located?
Fredrik Mörk
+1  A: 

You just have to set the property "Copy to Output Directory" of the report file to "Copy allways" or "Copy if newer". This will copy the report below the actual output directory during a build and you can access it with Path.Combine(Application.StartupPath, "Reports", "report.rpt").

Daniel Brückner
Thanks for the suggestion.i just output the stuff with a messagebox and see inside the debug folder.it's correct i 'haven't incorporated it in my project yet but i believe it would work.there is no Path.Join in my .Net 2.0 maybe on 3.5 but anyway i use the Path.combine.Thanks dude
black sensei
Fixed that ... Join is Python, I think...
Daniel Brückner
+2  A: 

You're not going to get the project folder from the compiled assembly because (as far as I know) it has no idea where it was compiled from. You are, however, going to get the folder where the assembly is running from, which is the "debug folder" you're getting from Application.ExecutablePath.

You would be better off to copy the reports into a Reports folder into the bin/debug folder and access them from there (you could use Application.ExecutablePath to get this). Then when you need to move the compiled files, you can move the Reports folder with it.

When you deploy, you would then have following structure:

C:\Program Files\proj1\proj1.exe
C:\Program Files\proj1\Reports\myreport.rpt

and not the following (which is what you're suggesting?):

C:\Program Files\proj1\proj1.exe
C:\Documents and Settings\myname\My Documents\Visual Studio 2005\Projects\proj1\Reports\myreport.rpt
lc
thanks for the reply.I used Daniel's method but yours and his are very similar.since i need to do further change to the report i'll like to stil have access through solution explorer i choose his.Thank you very much
black sensei