Alon M
Alon M

Reputation: 1683

Get Correct folder using c#

so, i am building a new WinForms with update my program.

the thing is, i am not installing any-thing. so,when i give my freinds my program, that can put it where ever they want. how can i know where did they put it?

like, lets say my program called "MyProg".

so lets say my freind puted "MyProg" in C:\programs\install\SayHello.

and i want my program to know where she is and save it to xml(everytimes she loads).

so, i know how to use everything here, i just need to know how can i get the folder path i am in now. (for my explined the foldepath = "C:\programs\install\SayHello.")

Anyone?

Thanks again, Alon. :)

Upvotes: 0

Views: 246

Answers (4)

Rich
Rich

Reputation: 15464

There are a few options, including:

Application.ExecutablePath

Search for "get exe location c#" for more variations on this.

Upvotes: 2

Jeremy McGee
Jeremy McGee

Reputation: 25200

From How do I get the name of the current executable in C#?, to find the name of the currently running assembly:

string file = object_of_type_in_application_assembly.GetType().Assembly.Location; string app = System.IO.Path.GetFileNameWithoutExtension(file);

so to find the path of the currently running assembly

string file = object_of_type_in_application_assembly.GetType().Assembly.Location; string path = System.IO.Path.GetDirectoryName(file);

should do the job.

Environment.CurrentDirectory won't necessarily return what you want, as it's possible to run the program from a different folder at the console.

Upvotes: 2

Felix C
Felix C

Reputation: 1775

Application.StartupPath

should do the same in your case.

Upvotes: 0

TJHeuvel
TJHeuvel

Reputation: 12618

Environment.CurrentDirectory contains the current directory.

Upvotes: 0

Related Questions