Get path 2 directories up from some known path

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,
Often times, I am interested in the path of a file within the project's root
directory instead of the bin/debug. To get this path, I am using the
following code snippet.

string path = Environment.CurrentDirectory;
string filepath = null;
for (int i = 1; i <= 3; i++) // try going up a few folders (like from
'Debug' or 'Release')
{
filepath = path + @"\" + "book1.xls";
if (File.Exists(filepath)) break;
int index = path.LastIndexOf(@"\", path.Length - 1);
if (index >= 0)
path = path.Substring(0, index);
}

A colleague of mine was making fun of code but then could not think of an
easier way of doing it. Any suggestions?

Thanks,
Dan
 
Dan,

I'd probably laugh at this too =)

Why not create a custom build action which will copy your file to the
output directory for the build? Since your logic in your program is
probably something similar to that, it's easier to have the build process
set up the output directory correctly instead of putting code in your
program that will change how it acts.

Hope this helps.
 
My only defense to the laughing is to justify that the code was me just
messing around. Laugh away though, wont be the first nor the last!

I do appreciate your answer but the true question is how to:

Get path 2 directories up from some known path, withouth having to interate
backwards like the code shown. I am just feeling there is an easier way to
"move 2 directories up from here".

Any ideas?

Thanks,
Dan

Nicholas Paldino said:
Dan,

I'd probably laugh at this too =)

Why not create a custom build action which will copy your file to the
output directory for the build? Since your logic in your program is
probably something similar to that, it's easier to have the build process
set up the output directory correctly instead of putting code in your
program that will change how it acts.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Hello,
Often times, I am interested in the path of a file within the project's
root directory instead of the bin/debug. To get this path, I am using
the following code snippet.

string path = Environment.CurrentDirectory;
string filepath = null;
for (int i = 1; i <= 3; i++) // try going up a few folders (like from
'Debug' or 'Release')
{
filepath = path + @"\" + "book1.xls";
if (File.Exists(filepath)) break;
int index = path.LastIndexOf(@"\", path.Length - 1);
if (index >= 0)
path = path.Substring(0, index);
}

A colleague of mine was making fun of code but then could not think of an
easier way of doing it. Any suggestions?

Thanks,
Dan
 
Dan,

You can always use the following path:

<current path>\..\..

That should give you two directories "up".


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

My only defense to the laughing is to justify that the code was me just
messing around. Laugh away though, wont be the first nor the last!

I do appreciate your answer but the true question is how to:

Get path 2 directories up from some known path, withouth having to
interate backwards like the code shown. I am just feeling there is an
easier way to "move 2 directories up from here".

Any ideas?

Thanks,
Dan

Nicholas Paldino said:
Dan,

I'd probably laugh at this too =)

Why not create a custom build action which will copy your file to the
output directory for the build? Since your logic in your program is
probably something similar to that, it's easier to have the build process
set up the output directory correctly instead of putting code in your
program that will change how it acts.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Hello,
Often times, I am interested in the path of a file within the project's
root directory instead of the bin/debug. To get this path, I am using
the following code snippet.

string path = Environment.CurrentDirectory;
string filepath = null;
for (int i = 1; i <= 3; i++) // try going up a few folders (like from
'Debug' or 'Release')
{
filepath = path + @"\" + "book1.xls";
if (File.Exists(filepath)) break;
int index = path.LastIndexOf(@"\", path.Length - 1);
if (index >= 0)
path = path.Substring(0, index);
}

A colleague of mine was making fun of code but then could not think of
an easier way of doing it. Any suggestions?

Thanks,
Dan
 
This will do it

string strNewDir =
System.Text.RegularExpressions.Regex.Replace(Environment.CurrentDirectory,
@"\\([^\\])+\\([^\\])+(\\)?$", "");
 

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

Back
Top