black sensei
black sensei

Reputation: 6678

Winform root directory path.How to again!

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

Upvotes: 2

Views: 16177

Answers (4)

Daniel Brückner
Daniel Brückner

Reputation: 59685

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").

Upvotes: 5

Tanmay
Tanmay

Reputation: 1165

string path =new DirectoryInfo(Environment.CurrentDirectory).Parent.Parent.FullName

Upvotes: 3

lc.
lc.

Reputation: 116528

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

Upvotes: 3

Fredrik Mörk
Fredrik Mörk

Reputation: 158359

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")

Upvotes: 2

Related Questions