syntax for app.startuppath to move back one folder

  • Thread starter Thread starter Rich
  • Start date Start date
R

Rich

Console.Writeline(Application.StartupPath);
returns
C:\WinApp3\bin\Debug

I want to go back to

C:\WinApp3\bin

I tried
Console.Writeline(Application.StartupPath + "\\..");

but this only returned
C:\WinApp3\bin\Debug\..

What is the correct syntax to return
C:\WinApp3\bin

Thanks,
Rich
 
Thank you. This works for what I need. But if I may ask one more:

If I want to move back 2 folders to C:\WinApp3

Sorry that I am not quite intuitive enough yet to figure this out. What
would that syntax look like?
 
What you can do is append "\..\..\" to the end of your path, and then do a
GetFullPath (I think -- or some such operation) to normalize it, or use it
as-is.
 
A better solution here would be to use the methods on the Path class in
the System.IO namespace.

First, combine the path:

string path = Path.Combine(Application.StartupPath, "..");

Then, get the full path:

path = Path.GetFullPath(path);

This will return the path, with the current and parent path indicators
resolved. This is a much better solution than doing it yourself with
substrings and whatnot, as that is probably more prone to error.
 
Rich said:
Console.Writeline(Application.StartupPath);
returns
C:\WinApp3\bin\Debug

I want to go back to

C:\WinApp3\bin

I tried
Console.Writeline(Application.StartupPath + "\\..");

but this only returned
C:\WinApp3\bin\Debug\..

What is the correct syntax to return
C:\WinApp3\bin

Thanks,
Rich

Just to add to the many ways listed, going up one level:
string s1 = new DirectoryInfo(Application.StartupPath).Parent.FullName;

and going up two levels:
string s2 = new DirectoryInfo(Application.StartupPath).Parent.Parent.FullName;
 
Back
Top