[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Get project folder path from a Test Project

Posted on 2011-09-12
12
Medium Priority
?
544 Views
Last Modified: 2012-05-12
Hi guys!

I need some help to get the executing folder path from a test project.
c:\projects\myapplication\MyTests\bin\Debug\mytests.dll

So when retrieving the path in my Test class

 [ClassInitialize]
 public static void ClassInit(TestContext context)
{
     var dir = ?????;
}

I need to get
c:\projects\myapplication\MyTests\bin\Debug\

Plz test it before posting, because I have already tested around 20 diffrent methods, but all returns the path with the TestResults folder incuded.
c:\projects\myapplication\MyTests\TestResults\xxxxxx_2011-09-12 13_43_43\Out

Thx
0
Comment
Question by:ulf-jzl
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 3
  • 2
12 Comments
 
LVL 23

Expert Comment

by:Snarf0001
ID: 36522264
What project type?  For winforms I use

System.Windows.Forms.Application.StartupPath

in just about every project I have and it returns the /debug folder.
0
 

Author Comment

by:ulf-jzl
ID: 36522400
"Test Project"  as it says ;)
0
 
LVL 23

Expert Comment

by:Snarf0001
ID: 36522482
I guess that shoudl have been obvious ;)
Bad habits I get into often making simple test projects out of other types.

In any case, using reflection and getting the executing assembly should work for you:

string startupPath = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:ulf-jzl
ID: 36522545
It does not work,, still the TestResults folder is included.
0
 
LVL 23

Assisted Solution

by:Snarf0001
Snarf0001 earned 800 total points
ID: 36522574
?? From ClassInit?  Are you sure you're not resetting it somewhere else?

What does

Assembly.GetExecutingAssembly().CodeBase

return?  It won't be a properly formatted file path, but should be a file:/// url right to the .dll.
0
 

Author Comment

by:ulf-jzl
ID: 36522798
Assembly.GetExecutingAssembly().CodeBase

"file:///C:/WS/MyProject/Release/Rel-v1.0/Source/MyProject/TestResults/xxxxxx_2011-09-12 15_52_07/Out/MyProject.DLL"      
0
 
LVL 20

Accepted Solution

by:
Daniel Van Der Werken earned 1200 total points
ID: 36522836
The problem is that you're deploying the test.

In the IDE, from the menu bar, select Test | Edit Test Run Configurations | Local Test Run

In the dialog, select Deployment.  Uncheck the "Enable deployment" checkbox.

Now, the things you've been trying should work, including the .CodeBase solution provided above.
0
 

Author Comment

by:ulf-jzl
ID: 36528422
Thx, works perfect!

But what is the Deployment thing for?
0
 

Author Comment

by:ulf-jzl
ID: 36528453
why I need to get that path is I'm storing some xml files that I use for testing in my test project folder, and I do not want to hard code in the path.

But maybe there is another good way to do this?
0
 

Author Comment

by:ulf-jzl
ID: 36528569
This works really well. If I do what Dan7el says.

Found this.
http://msdn.microsoft.com/en-us/library/ms182475.aspx
"If you deploy your tests to a separate folder, you can continue to build your solution while your tests are running."
Don't know if this is anything to care about, when you have projects that takes 1-5 min to build?

But maybe there is a better way to move my xml files to a specifc folder for testing. So I don't need to turn off the Deployment.

Now I like to move it from
c:\projects\myapplication\MyTests\-= Test Data =-
to
c:\Unittesting\XmlData\

Any better way to do this?

public static string AssemblyDirectory
{
    get
    {
        string codeBase = Assembly.GetExecutingAssembly().CodeBase;
        UriBuilder uri = new UriBuilder(codeBase);
        string path = Uri.UnescapeDataString(uri.Path); 
                
        return Path.GetDirectoryName(path);
    }
}

Open in new window

0
 
LVL 20

Expert Comment

by:Daniel Van Der Werken
ID: 36529434
Well, I don't know if you want to run though all that code each time you access the property.

I would do:

private static string assemblyDirectory = string.Empty;

public static string AssemblyDirectory
{
    get
    {
       if ( string.IsNullOrEmpty( assemblyDirectory ) )
        {
        string codeBase = Assembly.GetExecutingAssembly().CodeBase;
        UriBuilder uri = new UriBuilder(codeBase);
        string path = Uri.UnescapeDataString(uri.Path);
               
        assemblyDirectory = Path.GetDirectoryName(path);
       }
       return assemblyDirectory;
    }
}

Otherwise, really you can do this whatever you feel is best.  
0
 

Author Closing Comment

by:ulf-jzl
ID: 36534509
Thx guys! :)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question