Reputation: 1300
I'm trying to get an image from a certain path, but the path must only be referenced from the application's current folder and not from the Local C: drive.
My reason for this is because the application is going to get published soon and I can't reference the image to the same local location on my current PC, because the application is going to be used on a lot of other PC's and the path would not work on other someone else's computer.
This is the current path that works:
SetDefaultImage(new Binary(File.ReadAllBytes("C:\\Users\\mclaasse\\Desktop\\Haze Update\\Haze\\Haze\\Icons\\user6.jpg")));
And this is how I need it to be:
SetDefaultImage(new Binary(File.ReadAllBytes("Haze\\Icons\\user6.jpg")));
But i'm getting the error:
Could not find a part of the path 'C:\Haze\Icons\user6.jpg'.
Is there a simple work around for getting my path to work?
Upvotes: 3
Views: 2325
Reputation: 1300
None of the references worked for me(I don't know why), but this worked:
string directory = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
string filePath = Path.Combine(directory, "Icons\\user6.jpg");
Then check if the file that you are looking for exists:
if (!File.Exists(filePath))
{
MessageBox.Show("The default image does not exist", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
else
{
...
}
Upvotes: 1
Reputation: 128061
Not sure if this is exactly what you need, but provided that you have an image file user6.jpg
in your Visual Studio project in a project folder named Images
, and the Build Action
of the image file is set to Resource
, you could simply load it from a Resource File Pack URI:
var image = new BitmapImage(new Uri("pack://application:,,,/Images/user6.jpg"));
Upvotes: 2