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 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
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
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
This will do it
string strNewDir =
System.Text.RegularExpressions.Regex.Replace(Envir onment.CurrentDirectory,
@"\\([^\\])+\\([^\\])+(\\)?$", ""); This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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:...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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...
|
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,...
|
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...
|
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,...
| |