Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Project (bin) folder path at compile time?

Is there a way to find out the project path at compile time?

I want to create a unit test that tests if the configurartion in the default web.config (the one in the project folder). Mainly to reduce human error.

I cannot rely on assembly locations at runtime (for the test), so I need to know where the project folder is to access web.config there.

I need a "generic" solution since I'd like to use the same (base) test code for multiple projects, and the physical location is different anyway for most development machines.

Thanks.

like image 969
Inferis Avatar asked Apr 06 '09 20:04

Inferis


People also ask

What is bin folder in programming?

The bin folder holds binary files, which are the actual executable code for your application or library. Each of these folders are further subdivided into Debug and Release folders, which simply correspond to the project's build configurations.

How do I change the path of a project?

In Visual Studio, click Tools > Options. Expand Projects and Solutions and click Locations. The Projects location field defines the default location for storing new projects. You can change this path if you are using a different working folder.

How do I change the output path in Visual Studio?

Right-click on the project node in Solution Explorer and select Properties. Expand the Build section, and select the Output subsection. Find the Base output path for C#, and type in the path to generate output to (absolute or relative to the root project directory), or choose Browse to browse to that folder instead.

Where is the bin folder Visual Studio?

You can open the output bin directory on any project node in Visual Studio by simply right-clicking the project node and selected the "Open bin Folder (Explorer)" menu option.


2 Answers

Based on rkb's answer,

As it sounds like you've got a C# project, use this post build step.

echo namespace ProjectPath { static public class ProjectPath { public static readonly string Path = @"$(ProjectDir)";} } > $(ProjectDir)path.cs

Then include path.cs as an existing item to your test project. Then you can access it via:

string path = ProjectPath.ProjectPath.Path;
like image 64
Simeon Pilgrim Avatar answered Oct 12 '22 05:10

Simeon Pilgrim


If you want the Visual Studio project path, at compile time, you could use a Pre-Build Event (see the Project Properties dialog) to run a command line that will create a source file used in your project.

The source file will contain some code, say a variable definition. Your testing code uses this variable. The value of the variable will come from VS; when it runs your Pre-Build Event command, it substitutes project properties for certain macros. The macro you want is probably ProjectDir.

So in the end, you have something like this for your Pre-Build Event's command:

echo 'const char * PROJECT_PATH = "$(ProjectDir)";' > source.cpp

Not sure what language you're using, so adjust accordingly.

like image 21
rkb Avatar answered Oct 12 '22 06:10

rkb