简体   繁体   中英

Getting the application's directory from a WPF application

我找到了带有 AppDomain 的 Windows 窗体的解决方案,但是 WPF Application对象的等价物是什么?

One method:

System.AppDomain.CurrentDomain.BaseDirectory

Another way to do it would be:

System.IO.Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName)

这是另一个:

System.Reflection.Assembly.GetExecutingAssembly().Location

您还可以使用命令行参数的第一个参数:

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

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

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

string baseDir = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
String exePath = System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName;
 string dir = Path.GetDirectoryName(exePath);

Try this!

I tried this:

    label1.Content = Directory.GetCurrentDirectory();

and get also the directory.

您也可以自由使用 System.Windows.Forms 中的 Application.StartupPath,但必须为 System.Windows.Forms 程序集添加引用!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM