0

I have 3 application (asp.net website, console application, SSIS) and a common class library which is used by all these 3 application. That common library fetch the external configuration file which is located in the the current running directory. For example

ASP.NET - > c:\MySite\appconfig.xml

CONSOLE -> C:\MyConsoleApplication\appconfig.xml

SSIS -> c:\MySSISPakage\appconfig.xml

I used the following code to fetch the current running application path to fetch the appconfig.xml file

string appPath = AppDomain.CurrentDomain.BaseDirectory;

In the web application it is running file and returning the path

c:\MySite.

But in the console application it is return the path

C:\MyConsoleApplication\bin\debug

. I need to fetch the path

C:\MyConsoleApplication

for the console application. Any idea?

dotnetom
  • 24,551
  • 9
  • 51
  • 54
Pankaj Saha
  • 869
  • 3
  • 17
  • 37
  • see if this link helps http://stackoverflow.com/questions/15653921/get-current-folder-path – Vinkal Feb 18 '15 at 05:31
  • 2
    The problem is that the console app will return the folder that it is executing in which is currently the bin\debug folder. If you moved the console app to another folder what would you expect it to return? – Steve Dowling Feb 18 '15 at 05:32
  • The app is indeed running in that folder (you're running it in debug mode right?) When you build and deploy the app and run it from the appropriate folder it will reflect that folder. You need to add something to your debug setup script that copies your config file into the debug folder so all runs transparently. – Nick.Mc Feb 18 '15 at 05:47
  • You can set 'Copy to Output Directory' setting of the xml file to 'Copy if newer' to get the file in the debug or release folder. – Ako Feb 18 '15 at 08:42

1 Answers1

0

You can Find Parent Directory using Directory.GetParent

try like this: Program is name of any class in your project

string path = Path.GetDirectoryName(Assembly.GetAssembly(typeof(Program)).Location);

Console.WriteLine(Directory.GetParent(Directory.GetParent(path).ToString()).ToString());
Dgan
  • 10,077
  • 1
  • 29
  • 51
  • This is working fine for the console application, I have webapplication and SSIS package also and DLL is the same. Above code will be that common DLL which will be used by all the 3 types of applications. – Pankaj Saha Feb 19 '15 at 04:19