3 Replies - 228 Views - Last Post: 06 June 2019 - 06:57 AM Rate Topic: -----

#1 ScottinTexas   User is offline

  • D.I.C Regular

Reputation: 10
  • View blog
  • Posts: 300
  • Joined: 13-March 12

AppDomain.CurrentDomain.BaseDirectory Returns Different Results

Posted 05 June 2019 - 09:11 AM

Hi All,

When I run a unit test, AppDomain.CurrentDomain.BaseDirectory returns;

c:\Users\Me\Documents\Visual Studio 2017\Projects\TechDataCaptureTests\bin\Debug

When I run the regular code (debug mode) the same line is returned except it includes a final \.

c:\Users\Me\Documents\Visual Studio 2017\Projects\TechDataCapture\bin\Debug\

Of course, all of the special characters are escaped with a \. So when I add the filename to the string to open a file in one case I have ...Debug\Myfile.xml and in the other case I have ...Debug\\Myfile.XML. Again it includes the escape characters but I left them out for clarity.

Any idea what causes the different results?

This post has been edited by ScottinTexas: 05 June 2019 - 09:12 AM


Is This A Good Question/Topic? 0
  • +

Replies To: AppDomain.CurrentDomain.BaseDirectory Returns Different Results

#2 h4nnib4l   User is offline

  • The Noid
  • member icon

Reputation: 1399
  • View blog
  • Posts: 1,989
  • Joined: 24-August 11

Re: AppDomain.CurrentDomain.BaseDirectory Returns Different Results

Posted 05 June 2019 - 09:28 AM

I'm not sure what's causing that, but using Path.Combine(baseDir, filename) should resolve the issue.
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6968
  • View blog
  • Posts: 23,684
  • Joined: 05-May 12

Re: AppDomain.CurrentDomain.BaseDirectory Returns Different Results

Posted 05 June 2019 - 11:49 AM

It maybe an artifact of the unit testing framework you are using. Some of them setup separate app domains to provide isolation for the tests. Are you using MS Test, NUnit, xUnit, or something else? Does it change if you run your unit test from within Visual Studio as opposed to from the console (assuming your unit test framework has a console runner)?
Was This Post Helpful? 0
  • +
  • -

#4 ScottinTexas   User is offline

  • D.I.C Regular

Reputation: 10
  • View blog
  • Posts: 300
  • Joined: 13-March 12

Re: AppDomain.CurrentDomain.BaseDirectory Returns Different Results

Posted 06 June 2019 - 06:57 AM

I am only using VS 2017 for unit testing. I don't have any other. But The Noid's answer did work. So I'm good to go. Thank you both.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1