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


.



Relevant Pages

  • Re: filecopy
    ... Is the application file used in the code snippet (either source or ... Windows Embedded MVP ... > directories are in the root space from a smart device. ... > but it make's only an "..performed an illegal operation and will shut ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: How Web Service get container context???
    ... > I have a webservice that need to read files relative to the root of ... > the webapp that it is deployed in. ... ServletContext#getRealPath(String path) ...
    (comp.lang.java.programmer)
  • Configuration Question regarding Remoting, FormsBased Security, and Web.config
    ... I have a web structure that looks like this: ... I have set up my remoting information in the root Web.config ... string path = Context.ApplicationInstance.Server.MapPath; ...
    (microsoft.public.dotnet.framework.aspnet)
  • system() failed with $?==32512
    ... I have a program too large to post a code snippet, ... Any other ideas to narrow the root cause ... scope of this problem? ... SteveT ...
    (comp.lang.ruby)
  • Reading A TExt File
    ... If is read than I should be able to read the underlying tags. ... IF so can you help me with the code Snippet. ...
    (microsoft.public.dotnet.xml)