user310291
user310291

Reputation: 38228

How to get the true startup path at runtime of a c# console aplication

I created a console app, I put its shortcut in sendto directory.

Now I select in some OTHER directory a list of files to sendto shortcut and want to know what this other directory is.

I tried many things like Directory.GetCurrentDirectory and Environment.CurrentDirectory but it returns the path of my exe not the other directory where I selected my files.

So do you know how ?

Upvotes: 0

Views: 69

Answers (3)

Will Dean
Will Dean

Reputation: 39530

You need to look at the pathname(s) of the files being passed to your application by Windows.

From Windows PoV, there's nothing special about the directory the files are in; Windows is just passing the path of those files to your app. As the commenter above says, you can use members of the Path class to manipulate the paths and extract a directory name, if that's what you want.

There are three separate concepts here:

  • The directory where your .EXE is (doesn't change during execution)
  • The 'current' or 'working' directory (often the same as '1', but not necessarily) - may change while running - initially is under control of whoever launched your app.
  • The directories where files your application is working with reside - this could be many different things at different times in the lifetime of the app.

Upvotes: 1

GRUNGER
GRUNGER

Reputation: 496

Add to reference Windows.Forms:

using System.Windows.Forms;

and:

Application.StartupPath

Upvotes: 0

ironhide391
ironhide391

Reputation: 595

AppDomain.CurrentDomain.BaseDirectory

Upvotes: 0

Related Questions