Detect if the UnitTest is running and fix the AppDomain.BaseDirectory in VS 2008

The unit test in Visual Studio has a bug:
AppDomain.CurrentDomain.BaseDirectory always returns the installtion directory of the visual studio 2008 instead of the 'Out' directory.

So if your code have to access data files deployed to the 'Out' directory using relative path, error occurs.

A workaround is:

         public   static  GetApplicationBaseDirectory  {
            
if (AppDomain.CurrentDomain.FriendlyName.StartsWith("UnitTestAdapterDomain_")) {
                
return Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            }

                
return AppDomain.CurrentDomain.BaseDirectory;
        }


你可能感兴趣的:(Directory)