C# 从 WPF 应用程序获取应用程序的目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/938421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 03:47:03  来源:igfitidea点击:

Getting the application's directory from a WPF application

c#.netwpf

提问by Joey

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

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

采纳答案by Helen

One method:

一种方法:

System.AppDomain.CurrentDomain.BaseDirectory

Another way to do it would be:

另一种方法是:

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

回答by Arsen Mkrtchyan

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

Try this!

尝试这个!

回答by Eddie Butt

Here is another:

这是另一个:

System.Reflection.Assembly.GetExecutingAssembly().Location

回答by Flip

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

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

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

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

回答by crash

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

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

回答by Roshan J

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

尝试这个。不要忘记using System.Reflection

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

回答by QMaster

I used simply string baseDir = Environment.CurrentDirectory;and its work for me.

我简单地使用了string baseDir = Environment.CurrentDirectory;它,它对我有用。

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:

我曾经删除这种类型的错误,但我更喜欢编辑它,因为我认为这个答案的减号可以帮助人们了解错误的方式。:) 我知道上述解决方案没有用,我将其更改为string appBaseDir = System.AppDomain.CurrentDomain.BaseDirectory;其他获得它的方法是:

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

祝你好运

回答by paul

I tried this:

我试过这个:

    label1.Content = Directory.GetCurrentDirectory();

and get also the directory.

并获取目录。