0

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. :)

Vizel Leonid
  • 456
  • 7
  • 15
Alon M
  • 1,583
  • 7
  • 30
  • 44
  • 1
    possible duplicate of [How can I get the application's path in .NET in a console app?](http://stackoverflow.com/questions/837488/how-can-i-get-the-applications-path-in-net-in-a-console-app) – Jon Sep 08 '11 at 08:51
  • The linked answer doesn't work for ClickOnce: see http://stackoverflow.com/questions/616584/how-do-i-get-the-name-of-the-current-executable-in-c. – Jeremy McGee Sep 08 '11 at 08:58

4 Answers4

2

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.

Community
  • 1
  • 1
Jeremy McGee
  • 24,842
  • 10
  • 63
  • 95
  • 1
    Or more simply, use System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location). – Polyfun Sep 08 '11 at 09:36
2

There are a few options, including:

Application.ExecutablePath

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

Rich
  • 15,048
  • 2
  • 66
  • 119
0

Environment.CurrentDirectory contains the current directory.

TJHeuvel
  • 12,403
  • 4
  • 37
  • 46
0
Application.StartupPath

should do the same in your case.

Felix C
  • 1,755
  • 5
  • 26
  • 40
  • 1
    According to the other answer using assemblies, this would be probably better: System.Reflection.Assembly.GetExecutingAssembly().Location – Felix C Sep 08 '11 at 09:03
  • This is the "Start In" path used in shortcuts, and may not necessarily be where executable resides. – Gordon Bell Sep 04 '14 at 15:50