473,396 Members | 1,913 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Get path 2 directories up from some known path

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
Nov 17 '05 #1
4 9611
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]
- mv*@spam.guard.caspershouse.com

<Da********@nospam.nospam> wrote in message
news:OS**************@TK2MSFTNGP15.phx.gbl...
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

Nov 17 '05 #2
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:eU**************@TK2MSFTNGP09.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

<Da********@nospam.nospam> wrote in message
news:OS**************@TK2MSFTNGP15.phx.gbl...
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


Nov 17 '05 #3
Dan,

You can always use the following path:

<current path>\..\..

That should give you two directories "up".
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<Da********@nospam.nospam> wrote in message
news:uV**************@TK2MSFTNGP14.phx.gbl...
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:eU**************@TK2MSFTNGP09.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

<Da********@nospam.nospam> wrote in message
news:OS**************@TK2MSFTNGP15.phx.gbl...
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



Nov 17 '05 #4
This will do it

string strNewDir =
System.Text.RegularExpressions.Regex.Replace(Envir onment.CurrentDirectory,
@"\\([^\\])+\\([^\\])+(\\)?$", "");
Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Haim Ashkenazi | last post by:
Hi I have to write a function that returns a list of all directories under <PATH> (should work on linux and windows). it should be easy to write a loop that goes through every directory... and...
34
by: Reinhold Birkenfeld | last post by:
Hi, the arguments in the previous thread were convincing enough, so I made the Path class inherit from str/unicode again. It still can be found in CVS:...
3
by: Mark | last post by:
Hello all - I'm at wits end with trying to figure out why I'm having this problem since I followed the research, that I've dug up, to a "T". I'm trying to display a JPEG image from a path...
8
by: Lad | last post by:
Hello, what is a way to get the the extension of a filename from the path? E.g., on my XP windows the path can be C:\Pictures\MyDocs\test.txt and I would like to get the the extension of the...
1
by: Andreas Zita | last post by:
How do I get the absolute url for a file from c#-code? I have the path "~/file.aspx" and I want "http://www.mydomain.xyz/myapp/file.aspx" Basically I want to get the complete app-path url...
6
by: Soren | last post by:
Hi, I'd like to read the filenames in a directory, but not the subdirectories, os.listdir() gives me everything... how do I separate the directory names from the filenames? Is there another way...
0
by: Jeff McNeil | last post by:
Your args are fine, that's just the way os.path.walk works. If you just need the absolute pathname of a directory when given a relative path, you can always use os.path.abspath, too. A couple...
1
by: Stefan Pascal | last post by:
Hello. I have a very strange problem on my system with running C# applications from a longer than usual (>127 chars) path. I was able to reproduce the following 2 cases: 1. Socket s = new...
5
by: John A Grandy | last post by:
I don't find .net lib method to return the folder-path of a file-path. I'm probably not looking hard enough ... I've checked the System.IO.File,Path,Directory libs
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.