0

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

Phil
  • 561
  • 2
  • 16
  • 29
  • What do you mean by the *root of WPF App*? Do you mean the file path of the root folder of the startup project in a WPF App? – Sheridan Jun 24 '15 at 10:42
  • There is nothing like a "root directory" of a WPF application. If you are going to access files in the root folder of your Visual Studio project, you should set their Build Action to Content and get them copied to the build output directory (e.g. by setting Copy Always). You may also embed them as Resources in the application's assembly. Please explain what you are actually trying to achieve. – Clemens Jun 24 '15 at 10:46
  • Tell us what you are trying to achieve... it is more than likely that you are going about this the wrong way! - You sound like you are treating a symptom! – BenjaminPaul Jun 24 '15 at 10:49
  • I have actually part of filepaths (Images/FileName) stored in a DB and the images are in the image folder and not stored in the database. To make the bitmap URI work I need to combine the startup path with the Images/FileName. to do this I need the startup path as a string. I hope I make sense, but I feel there might be a better way – Phil Jun 24 '15 at 10:57

3 Answers3

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

Another way is:

Environment.CurrentDirectory 

if it was not changed.

keymusicman
  • 1,281
  • 1
  • 10
  • 20
0

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");
Sheridan
  • 68,826
  • 24
  • 143
  • 183
0

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);
Community
  • 1
  • 1
Clemens
  • 123,504
  • 12
  • 155
  • 268