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\insta开发者_如何学Cll\SayHello.")
Anyone?
Thanks again, Alon. :)
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.
There are a few options, including:
Application.ExecutablePath
Search for "get exe location c#" for more variations on this.
Environment.CurrentDirectory
contains the current directory.
Application.StartupPath
should do the same in your case.
精彩评论