Get Parent Directory

S

shapper

Hello,

On a C# file I need to get the current solution parent path.

But not the immediate path. I need to get 3 directories up.

Because I am not sure how to get the current Solution path I defined
the following:

String l = Assembly.GetExecutingAssembly().Location;

But now from this location it is even worse. I need to get the
directory from 6 levels up.

I have been using Path, Directory, etc ... But can't find a good way
to do this.

How can I do this?

Thanks,

Miguel
 
J

Jeff Johnson

On a C# file I need to get the current solution parent path.

Do you mean the folder that holds the .sln file? If so, there's no built-in
way to do this. You'll have to keep walking the tree backwards until you
find the file you're looking for.
 
S

shapper

Please be more specific.  The System.IO.Path class has static members to
help manipulate path strings, including retrieving all of the path
except the very last segment.  So you can just call GetDirectoryName()
multiple times to backtrack to the directory level of interest, relative
to a given path.

Are you using that method?  If so, why doesn't it do what you want.  If
not, you should try it.

Pete

I solved it using the following:
_codePath = Directory.GetParent("../../../../").FullName;

But to be honest I am not sure if this the best way.

I was looking for something like:

GetParent(String path, Int32 depth);

And I would return that Directory:

_codePath = IOHelpers.GetParent(currentPath, 4).FullName;

Does this make sense?
 
A

Arne Vajhøj

Something like this:

public DirectoryInfo GetParent(String path, Int32 depth) {
DirectoryInfo parent = Directory.GetParent(path);
return depth == 0 ? parent : GetParent(parent.FullName, depth - 1);
}

What do you think? Not sure if it is the best way but ...

The recursive approach looks good to me.

I think the semantics of depth argument will confuse people.

Arne
 
S

shapper

Can you tell us what you're trying to do? Are you making an add-on for the
VS IDE? If not, what is the use of finding the solution file?

Basically, I am backing up a SQL 2008 database using SQL Management
SMO.
But I need to save that backup up in a folder outside the solution.
So I need to go up a few nodes.

I don't need to get the solution path. I was just trying various
options to solve the problem.
 
S

shapper

The name is fine.

But the logic of:

0 meaning 1 level up
1 meaning 2 levels up

will confuse people.

Arne

Hi see. I didn't notice that. Well, here it is a new version:

public static DirectoryInfo GetParent(String path, Int32 depth) {

// Return current
if (depth == 0)
return new DirectoryInfo(path);

// Define parent
DirectoryInfo parent = Directory.GetParent(path);

// Return parent or get new parent
return depth == 1 ? parent : GetParent(parent.FullName, depth -
1);

} // GetParent

And now that I have been playing with Unit Testing:

// GetParent_DepthZero_IsCurrent
[TestMethod, TestCategory("Net")]
public void GetParent_DepthZero_IsCurrent() {

// Get parent
DirectoryInfo directory =
IOHelpers.GetParent(Directory.GetCurrentDirectory(), 0);

// Depth equal to zero should return current directory
Assert.AreEqual(Directory.GetCurrentDirectory(),
directory.FullName);

} // GetParent_DepthZero_IsCurrent

// GetParent_DepthOne_IsOneLevelUp
[TestMethod, TestCategory("Net")]
public void GetParent_DepthOne_IsOneLevelUp() {

// Get parent
DirectoryInfo directory =
IOHelpers.GetParent(Directory.GetCurrentDirectory(), 1);

// Depth equal to one should return the one level up directory

Assert.AreEqual(Directory.GetParent(Directory.GetCurrentDirectory()).FullName,
directory.FullName);

} // GetParent_DepthOne_IsOneLevelUp

// GetParent_DepthTwo_IsTwoLevelsUp
[TestMethod, TestCategory("Net")]
public void GetParent_DepthTwo_IsTwoLevelsUp() {

// Get parent
DirectoryInfo directory =
IOHelpers.GetParent(Directory.GetCurrentDirectory(), 2);

// Depth equal to two should return the two levels up directory

Assert.AreEqual(Directory.GetParent(Directory.GetParent(Directory.GetCurrentDirectory()).FullName).FullName,
directory.FullName);

} // GetParent_DepthTwo_IsTwoLevelsUp

A side question:

What do you think about my Unit Testing naming?

Thank You,
Miguel
 
A

Arne Vajhøj

Hi see. I didn't notice that. Well, here it is a new version:

public static DirectoryInfo GetParent(String path, Int32 depth) {

// Return current
if (depth == 0)
return new DirectoryInfo(path);

// Define parent
DirectoryInfo parent = Directory.GetParent(path);

// Return parent or get new parent
return depth == 1 ? parent : GetParent(parent.FullName, depth -
1);

} // GetParent

What about:

public static DirectoryInfo GetParent(String path, Int32 depth) {
if (depth == 0)
return new DirectoryInfo(path);
return GetParent(Directory.GetParent(path).FullName, depth - 1);
}

?
And now that I have been playing with Unit Testing:

// GetParent_DepthZero_IsCurrent
[TestMethod, TestCategory("Net")]
public void GetParent_DepthZero_IsCurrent() {

// Get parent
DirectoryInfo directory =
IOHelpers.GetParent(Directory.GetCurrentDirectory(), 0);

// Depth equal to zero should return current directory
Assert.AreEqual(Directory.GetCurrentDirectory(),
directory.FullName);

} // GetParent_DepthZero_IsCurrent

// GetParent_DepthOne_IsOneLevelUp
[TestMethod, TestCategory("Net")]
public void GetParent_DepthOne_IsOneLevelUp() {

// Get parent
DirectoryInfo directory =
IOHelpers.GetParent(Directory.GetCurrentDirectory(), 1);

// Depth equal to one should return the one level up directory

Assert.AreEqual(Directory.GetParent(Directory.GetCurrentDirectory()).FullName,
directory.FullName);

} // GetParent_DepthOne_IsOneLevelUp

// GetParent_DepthTwo_IsTwoLevelsUp
[TestMethod, TestCategory("Net")]
public void GetParent_DepthTwo_IsTwoLevelsUp() {

// Get parent
DirectoryInfo directory =
IOHelpers.GetParent(Directory.GetCurrentDirectory(), 2);

// Depth equal to two should return the two levels up directory

Assert.AreEqual(Directory.GetParent(Directory.GetParent(Directory.GetCurrentDirectory()).FullName).FullName,
directory.FullName);

} // GetParent_DepthTwo_IsTwoLevelsUp

A side question:

What do you think about my Unit Testing naming?

Maybe not so common with underscores, but fine IMHO.

Arne
 
S

shapper

What about:

public static DirectoryInfo GetParent(String path, Int32 depth) {
     if (depth == 0)
         return new DirectoryInfo(path);
     return GetParent(Directory.GetParent(path).FullName, depth - 1);

}

?

Yes, that seems better. Thank You.
And now that I have been playing with Unit Testing:
     // GetParent_DepthZero_IsCurrent
     [TestMethod, TestCategory("Net")]
     public void GetParent_DepthZero_IsCurrent() {
       // Get parent
       DirectoryInfo directory =
IOHelpers.GetParent(Directory.GetCurrentDirectory(), 0);
       // Depth equal to zero should return current directory
       Assert.AreEqual(Directory.GetCurrentDirectory(),
directory.FullName);
     } // GetParent_DepthZero_IsCurrent
     // GetParent_DepthOne_IsOneLevelUp
     [TestMethod, TestCategory("Net")]
     public void GetParent_DepthOne_IsOneLevelUp() {
       // Get parent
       DirectoryInfo directory =
IOHelpers.GetParent(Directory.GetCurrentDirectory(), 1);
       // Depth equal to one should return the one level up directory
Assert.AreEqual(Directory.GetParent(Directory.GetCurrentDirectory()).FullNa me,
directory.FullName);
     } // GetParent_DepthOne_IsOneLevelUp
     // GetParent_DepthTwo_IsTwoLevelsUp
     [TestMethod, TestCategory("Net")]
     public void GetParent_DepthTwo_IsTwoLevelsUp() {
       // Get parent
       DirectoryInfo directory =
IOHelpers.GetParent(Directory.GetCurrentDirectory(), 2);
       // Depth equal to two should return the two levels up directory
Assert.AreEqual(Directory.GetParent(Directory.GetParent(Directory.GetCurren tDirectory()).FullName).FullName,
directory.FullName);
     } // GetParent_DepthTwo_IsTwoLevelsUp
A side question:
What do you think about my Unit Testing naming?

Maybe not so common with underscores, but fine IMHO.

I have been searching for Unit Testing Naming Conventions and the most
common is:

NameOfMethodThatIsTesting_Action_ExpectedResult

So I adopted the following:

1 - Place Test Methods in a similar namespace as the classes that they
are testing. For example:
MyApp.Domain.Tests contains the tests for MyApp.Domain.Tests.

2 - Use categories for classify the methods

3 - Using the mentioned naming convention:
NameOfMethodThatIsTesting_Action_ExpectedResult

What convention for name do you use or is usually used? I would like
to know.

This is what I found.

Thank You,
Miguel
 
A

Arne Vajhøj

And now that I have been playing with Unit Testing:
// GetParent_DepthZero_IsCurrent
[TestMethod, TestCategory("Net")]
public void GetParent_DepthZero_IsCurrent() {
// Get parent
DirectoryInfo directory =
IOHelpers.GetParent(Directory.GetCurrentDirectory(), 0);
// Depth equal to zero should return current directory
Assert.AreEqual(Directory.GetCurrentDirectory(),
directory.FullName);
} // GetParent_DepthZero_IsCurrent
// GetParent_DepthOne_IsOneLevelUp
[TestMethod, TestCategory("Net")]
public void GetParent_DepthOne_IsOneLevelUp() {
// Get parent
DirectoryInfo directory =
IOHelpers.GetParent(Directory.GetCurrentDirectory(), 1);
// Depth equal to one should return the one level up directory
Assert.AreEqual(Directory.GetParent(Directory.GetCurrentDirectory()).FullNa me,
directory.FullName);
} // GetParent_DepthOne_IsOneLevelUp
// GetParent_DepthTwo_IsTwoLevelsUp
[TestMethod, TestCategory("Net")]
public void GetParent_DepthTwo_IsTwoLevelsUp() {
// Get parent
DirectoryInfo directory =
IOHelpers.GetParent(Directory.GetCurrentDirectory(), 2);
// Depth equal to two should return the two levels up directory
Assert.AreEqual(Directory.GetParent(Directory.GetParent(Directory.GetCurren tDirectory()).FullName).FullName,
directory.FullName);
} // GetParent_DepthTwo_IsTwoLevelsUp
A side question:
What do you think about my Unit Testing naming?

Maybe not so common with underscores, but fine IMHO.

I have been searching for Unit Testing Naming Conventions and the most
common is:

NameOfMethodThatIsTesting_Action_ExpectedResult

So I adopted the following:

1 - Place Test Methods in a similar namespace as the classes that they
are testing. For example:
MyApp.Domain.Tests contains the tests for MyApp.Domain.Tests.

2 - Use categories for classify the methods

3 - Using the mentioned naming convention:
NameOfMethodThatIsTesting_Action_ExpectedResult

What convention for name do you use or is usually used? I would like
to know.

It is not compatible with the general MS class library naming
convention.

And it is not what the unit test framework I use (NUnit) use.

But if it is the standard for the unit test framework you use,
then it is fine.

Arne
 
S

shapper

On 15-10-2010 09:20, shapper wrote:
And now that I have been playing with Unit Testing:
      // GetParent_DepthZero_IsCurrent
      [TestMethod, TestCategory("Net")]
      public void GetParent_DepthZero_IsCurrent() {
        // Get parent
        DirectoryInfo directory =
IOHelpers.GetParent(Directory.GetCurrentDirectory(), 0);
        // Depth equal to zero should return current directory
        Assert.AreEqual(Directory.GetCurrentDirectory(),
directory.FullName);
      } // GetParent_DepthZero_IsCurrent
      // GetParent_DepthOne_IsOneLevelUp
      [TestMethod, TestCategory("Net")]
      public void GetParent_DepthOne_IsOneLevelUp() {
        // Get parent
        DirectoryInfo directory =
IOHelpers.GetParent(Directory.GetCurrentDirectory(), 1);
        // Depth equal to one should return the one level up directory
Assert.AreEqual(Directory.GetParent(Directory.GetCurrentDirectory()).FullNa me,
directory.FullName);
      } // GetParent_DepthOne_IsOneLevelUp
      // GetParent_DepthTwo_IsTwoLevelsUp
      [TestMethod, TestCategory("Net")]
      public void GetParent_DepthTwo_IsTwoLevelsUp() {
        // Get parent
        DirectoryInfo directory =
IOHelpers.GetParent(Directory.GetCurrentDirectory(), 2);
        // Depth equal to two should return the two levels updirectory
Assert.AreEqual(Directory.GetParent(Directory.GetParent(Directory.GetCurren tDirectory()).FullName).FullName,
directory.FullName);
      } // GetParent_DepthTwo_IsTwoLevelsUp
A side question:
What do you think about my Unit Testing naming?
Maybe not so common with underscores, but fine IMHO.
I have been searching for Unit Testing Naming Conventions and the most
common is:

So I adopted the following:
1 - Place Test Methods in a similar namespace as the classes that they
are testing. For example:
      MyApp.Domain.Tests contains the tests for MyApp.Domain.Tests.
2 - Use categories for classify the methods
3 - Using the mentioned naming convention:
      NameOfMethodThatIsTesting_Action_ExpectedResult
What convention for name do you use or is usually used? I would like
to know.

It is not compatible with the general MS class library naming
convention.

And it is not what the unit test framework I use (NUnit) use.

But if it is the standard for the unit test framework you use,
then it is fine.

Arne

But when using NUnit do you have a convention that you follow?

I would like to know ... I am still looking for the best approach.

Thank You,
Miguel
 
A

Arne Vajhøj

On 15-10-2010 09:20, shapper wrote:
And now that I have been playing with Unit Testing:
// GetParent_DepthZero_IsCurrent
[TestMethod, TestCategory("Net")]
public void GetParent_DepthZero_IsCurrent() {
// Get parent
DirectoryInfo directory =
IOHelpers.GetParent(Directory.GetCurrentDirectory(), 0);
// Depth equal to zero should return current directory
Assert.AreEqual(Directory.GetCurrentDirectory(),
directory.FullName);
} // GetParent_DepthZero_IsCurrent
// GetParent_DepthOne_IsOneLevelUp
[TestMethod, TestCategory("Net")]
public void GetParent_DepthOne_IsOneLevelUp() {
// Get parent
DirectoryInfo directory =
IOHelpers.GetParent(Directory.GetCurrentDirectory(), 1);
// Depth equal to one should return the one level up directory
Assert.AreEqual(Directory.GetParent(Directory.GetCurrentDirectory()).FullNa me,
directory.FullName);
} // GetParent_DepthOne_IsOneLevelUp
// GetParent_DepthTwo_IsTwoLevelsUp
[TestMethod, TestCategory("Net")]
public void GetParent_DepthTwo_IsTwoLevelsUp() {
// Get parent
DirectoryInfo directory =
IOHelpers.GetParent(Directory.GetCurrentDirectory(), 2);
// Depth equal to two should return the two levels up directory
Assert.AreEqual(Directory.GetParent(Directory.GetParent(Directory.GetCurren tDirectory()).FullName).FullName,
directory.FullName);
} // GetParent_DepthTwo_IsTwoLevelsUp
A side question:
What do you think about my Unit Testing naming?
Maybe not so common with underscores, but fine IMHO.
I have been searching for Unit Testing Naming Conventions and the most
common is:

So I adopted the following:
1 - Place Test Methods in a similar namespace as the classes that they
are testing. For example:
MyApp.Domain.Tests contains the tests for MyApp.Domain.Tests.
2 - Use categories for classify the methods
3 - Using the mentioned naming convention:
NameOfMethodThatIsTesting_Action_ExpectedResult
What convention for name do you use or is usually used? I would like
to know.

It is not compatible with the general MS class library naming
convention.

And it is not what the unit test framework I use (NUnit) use.

But if it is the standard for the unit test framework you use,
then it is fine.

But when using NUnit do you have a convention that you follow?

The NUnit convention is just using a simple name in traditional
XxxXxxXxx style describing what is being tested.
I would like to know ... I am still looking for the best approach.

Coding conventions are just conventions. The best approach is
to follow the convention that the company prescribe and
if no such exist then follow the convention suggested
by the framework.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top