I found solutions for Windows Forms with AppDomain but what would be the equivalent for a WPF Application
object?
9 Answers
One method:
System.AppDomain.CurrentDomain.BaseDirectory
Another way to do it would be:
System.IO.Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName)
-
Ah, thanks. Must have overlooked AppDomain somehow. I was looking for it, actually ...– JoeyJun 2, 2009 at 12:34
-
3@Helen: Judging from the upvotes, this is obviously an excellent answer. However, the answer has two ways of getting the app dir. Will they both work equally well? Aug 29, 2011 at 15:34
-
24I would use the first alternative. It looks simpler, doesn't have a method call and causes less doubt on what the line actually does when reading.– FilipOct 21, 2011 at 3:11
-
4I've used the first method in a WCF service, in a WPF Application and in a Class Library project and worked fine in all. Nov 25, 2015 at 12:21
-
1For NetCore and NetStandard, I would recommend the second one, because AppDomain was added in 2.0 and could not be always set as expected– cdieOct 5, 2017 at 13:22
Here is another:
System.Reflection.Assembly.GetExecutingAssembly().Location
-
2This one gets the location after shadow-copying, as stated in the docs. I'm actually not sure if the suggestions in the accepted answer is affected by shadow-copying. Sep 7, 2011 at 8:02
-
2This gave me the sub directory of the DLL I was calling, not the main program directory. Jun 16, 2014 at 9:14
You can also use the first argument of the command line arguments:
String exePath = System.Environment.GetCommandLineArgs()[0]
-
1However, note that an "evil" application can modify its command line arguments. Apr 20, 2011 at 14:34
-
@Daniel: Why would it do it to itself? Or do you mean a different application? Aug 28, 2011 at 18:50
-
1@Merlyn: See blogs.msdn.com/b/oldnewthing/archive/2009/11/25/9928372.aspx I'll quote: it is a "conveniently initialized parameter to the process's startup code." So you can deliberately or inadvertently modify that memory location. Aug 29, 2011 at 7:46
-
1@Daniel: Who can? Another process, or the same process? If you shoot yourself in the foot, it should be easy to track down. I'd call that less evil, and more stupid :) If another process can do it, then that is more interesting. Edit: I don't see anything in that article about modifying a running program's command line - only that the launching process passes it in (not sure it is undesirable for the launching process to change the command line), and that you can query it via WMI. Aug 29, 2011 at 17:44
-
@MerlynMorgan-Graham a malicious application can modify the memory address, and force your application to run another one of their malicious application instead. This is evil, because let's say their application is a keylogger and they want it to activate as soon as you open up a sepcific application. So in their code they'll capture your application name, run their keylogger in the background, and then run your application. The user will be clueless. The article does explain this, but it doesn't tell you how to do it. Maybe that's what you were looking for?– pqskJun 26, 2014 at 14:53
I used simply string baseDir = Environment.CurrentDirectory;
and its work for me.
Good Luck
Edit:
I used to delete this type of mistake but i prefer to edit it because i think the minus point on this answer help people to know about wrong way. :) I understood the above solution is not useful and i changed it to string appBaseDir = System.AppDomain.CurrentDomain.BaseDirectory;
Other ways to get it are:
1. string baseDir =
System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
2. String exePath = System.Environment.GetCommandLineArgs()[0];
3. string appBaseDir = System.IO.Path.GetDirectoryName
(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);
Good Luck
-
3That's the current working directory. It may conincide with the application directory but those are separate concepts (and most importantly, the working directory can change, e.g. if you had a common file dialog open).– JoeyDec 12, 2012 at 10:11
-
1@joey you are right. I changed it to this: string appBaseDir = System.AppDomain.CurrentDomain.BaseDirectory; Thanks.– QMasterDec 23, 2012 at 6:56
String exePath = System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName;
string dir = Path.GetDirectoryName(exePath);
Try this!
Try this. Don't forget using System.Reflection
.
string baseDir = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
I tried this:
label1.Content = Directory.GetCurrentDirectory();
and get also the directory.
Just thought I'd add an updated answer for those who need it.
I used to use: My.Application.Info.DirectoryPath
to get my applications path. It seems NET6 didn't like this. After using one of the examples below, I noticed IntelliSense suggested this: Environment.ProcessPath
Thus, to get the path to the application exe:
Environment.ProcessPath
To get the folder:
Path.GetDirectoryName(Environment.ProcessPath)
Hope this helps.
-
My
is part of Visual Basic, so perhaps your C# project just has to reference the relevant VB assembly explicitly.– JoeyApr 4, 2022 at 8:44 -
-
My still exists as when using VB with .NET framework, but with .NET (.NET 5, .NET 6, etc) it is mostly removed. Haven't checked .NET core, .NET standard, etc. Also, gotta love the naming strategy MS had with the entire .NET franchise. OG is .NET framework, while .net is the newest iteration.– Wolf-KunFeb 22 at 17:26
You can also use freely Application.StartupPath from System.Windows.Forms, but you must to add reference for System.Windows.Forms assembly!