185

I found solutions for Windows Forms with AppDomain but what would be the equivalent for a WPF Application object?

0

9 Answers 9

366

One method:

System.AppDomain.CurrentDomain.BaseDirectory

Another way to do it would be:

System.IO.Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName)
9
  • Ah, thanks. Must have overlooked AppDomain somehow. I was looking for it, actually ...
    – Joey
    Jun 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
  • 24
    I 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.
    – Filip
    Oct 21, 2011 at 3:11
  • 4
    I've used the first method in a WCF service, in a WPF Application and in a Class Library project and worked fine in all.
    – Apostrofix
    Nov 25, 2015 at 12:21
  • 1
    For NetCore and NetStandard, I would recommend the second one, because AppDomain was added in 2.0 and could not be always set as expected
    – cdie
    Oct 5, 2017 at 13:22
32

Here is another:

System.Reflection.Assembly.GetExecutingAssembly().Location
2
  • 2
    This 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
  • 2
    This gave me the sub directory of the DLL I was calling, not the main program directory.
    – strattonn
    Jun 16, 2014 at 9:14
8

You can also use the first argument of the command line arguments:

String exePath = System.Environment.GetCommandLineArgs()[0]

9
  • 1
    However, 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?
    – pqsk
    Jun 26, 2014 at 14:53
7

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

2
  • 3
    That'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).
    – Joey
    Dec 12, 2012 at 10:11
  • 1
    @joey you are right. I changed it to this: string appBaseDir = System.AppDomain.CurrentDomain.BaseDirectory; Thanks.
    – QMaster
    Dec 23, 2012 at 6:56
3
String exePath = System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName;
 string dir = Path.GetDirectoryName(exePath);

Try this!

0
3

Try this. Don't forget using System.Reflection.

string baseDir = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
1

I tried this:

    label1.Content = Directory.GetCurrentDirectory();

and get also the directory.

2
  • This gets the current working directory, which can be different from the process directory.
    – Thraka
    Jun 6, 2014 at 19:35
  • I guess I have to evaluate Directory.GetCurrentDirectory(); more. Thank you @Thraka for correction.
    – paul
    Aug 11, 2014 at 4:10
1

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.

3
  • My is part of Visual Basic, so perhaps your C# project just has to reference the relevant VB assembly explicitly.
    – Joey
    Apr 4, 2022 at 8:44
  • Yes, VB. I don't do C#.
    – video.baba
    Apr 4, 2022 at 10:55
  • 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-Kun
    Feb 22 at 17:26
0

You can also use freely Application.StartupPath from System.Windows.Forms, but you must to add reference for System.Windows.Forms assembly!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.