Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the current directory on a class library?

I've been looking around but I have not found a solution for this problem: I want to create a class library that has a configuration file under a sub-directory called Configuration. I want that class library to be deployed anywhere and I want it to find its configuration files by knowing its own location.

Previous attempts with Assembly.GetExecutingAssembly().Location did not work.
It would return temp locations such as

C:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\7c00e0a3\38789d63\assembly\dl3\9c0a23ff\18fb5feb_6ac3c901

instead of the desired

bin/Configuration path.

So:

  1. Can a class library be aware of its own location on disk?
  2. How would I go about witting test scripts for this functionality since it seems that directories change based on how you run the app (debugging inside VS, deploying on IIS, etc)
like image 614
nandos Avatar asked Apr 22 '09 17:04

nandos


People also ask

How do I get the current directory in VB net?

You can use WinDir variable to get the current directory. The Environment. GetEnvironmentVariable method can be used for that. Here is the code snippet that gets the current directory using VB.NET.

How do I find the project path in Visual Studio?

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.


2 Answers

This should work -

string assemblyFile = (     new System.Uri(Assembly.GetExecutingAssembly().CodeBase) ).AbsolutePath; 
like image 128
Reed Copsey Avatar answered Sep 21 '22 00:09

Reed Copsey


The below code worked for me to get the physical path of the Images folder in-class library file.

string fullFilePath = Path.Combine((new System.Uri(Assembly.GetExecutingAssembly().CodeBase)).AbsolutePath.Split(new string[] { "/bin" }, StringSplitOptions.None)[0]                           , "@/Images/test.png"); 

I hope, it will help someone.

like image 38
Rajeev Kumar Avatar answered Sep 24 '22 00:09

Rajeev Kumar