找到你要的答案

Q:getting root of wpf app as a string

Q:得到WPF应用程序根作为字符串

Is there another way to get the root of a wpf application as a string? Now I'm still using

string path = AppDomain.CurrentDomain.BaseDirectory.Substring(0, (AppDomain.CurrentDomain.BaseDirectory.Length - 10));

This gives me the root as a string, but I assume this is not the right way to do it.

I also tried

string txt = System.AppDomain.CurrentDomain.BaseDirectory.ToString();

but this sends me to root/bin/debug. I only need the root as a string

Is there another way to get the root of a wpf application as a string? Now I'm still using

string path = AppDomain.CurrentDomain.BaseDirectory.Substring(0, (AppDomain.CurrentDomain.BaseDirectory.Length - 10));

这给了我根作为一个字符串,但我认为这不是正确的方法来做它。

我也试过

string txt = System.AppDomain.CurrentDomain.BaseDirectory.ToString();

but this sends me to root/bin/debug. I only need the root as a string

answer1: 回答1:

Orginal Threat is here

var path=System.Reflection.Assembly.GetExecutingAssembly().Location

returns where the executing assembly is currently located, which may or may not be where the assembly is located when not executing. In the case of shadow copying assemblies, you will get a path in a temp directory.

 `var path=System.Reflection.Assembly.GetExecutingAssembly().CodeBase`

will return the 'permenant' path of the assembly

这里是原始的威胁

var path=System.Reflection.Assembly.GetExecutingAssembly().Location

returns where the executing assembly is currently located, which may or may not be where the assembly is located when not executing. In the case of shadow copying assemblies, you will get a path in a temp directory.

 `var path=System.Reflection.Assembly.GetExecutingAssembly().CodeBase`

将返回该组件的“永久”路径

answer2: 回答2:
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)

Another way is:

Environment.CurrentDirectory 

if it was not changed.

Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)

另一种方法是:

Environment.CurrentDirectory 

如果没有改变。

answer3: 回答3:

You can find the file path of the root folder of the startup project in a WPF App like this:

string applicationDirectory = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
string rootPath = Directory.GetParent(applicationDirectory).Parent.FullName;

Or to complete your example by getting the file path of the parent folder of the parent folder, you can do this:

string rootPath = Directory.GetParent(txt).Parent.FullName;

UPDATE >>>

In order to access your project Images folder, you can do this:

Path.Combine(Directory.GetParent(applicationDirectory).Parent.FullName, "Images");

你可以找到在一个WPF应用程序这样的启动项目的根文件夹中的文件路径:

string applicationDirectory = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
string rootPath = Directory.GetParent(applicationDirectory).Parent.FullName;

或者通过获取父文件夹的父文件夹的文件路径来完成您的示例,您可以这样做:

string rootPath = Directory.GetParent(txt).Parent.FullName;

更新>;>;>;

为了访问您的项目图像文件夹,您可以这样做:

Path.Combine(Directory.GetParent(applicationDirectory).Parent.FullName, "Images");
answer4: 回答4:

You should put the images in a folder of your Visual Studio project called "Images" and set their Build Action to Resource (as shown here).

If you then get a relative image path from your DB, you would create a Pack URI and load a BitmapImage like this:

var imagePath = "Images/SomeImage.jpg"; // actually from DB
var uri = new Uri("pack://application:,,,/" + imagePath);
var bitmap = new BitmapImage(uri);

您应该将图像放在Visual Studio项目的文件夹中,称为“图像”,并将其生成操作设置为资源(如图所示)。

如果你从你的数据库到一个相对的图像的路径,可以创建一个包的URI和负荷bitmapimage这样:

var imagePath = "Images/SomeImage.jpg"; // actually from DB
var uri = new Uri("pack://application:,,,/" + imagePath);
var bitmap = new BitmapImage(uri);
c#  wpf  appdomain