Reputation: 1949
I am trying to detect if a given file exists. For example, let's say I'm looking for Joystick.png in this directory: F:\FrontEnd\Themes\Custom\Images\Platform\Controller\Joystick.png
I cannot hard code the directory structure because the application might be installed elsewhere. the directory structure however, doesn't change.
I had been using this
string path = $"pack://siteoforigin:,,,/Themes/Custom/Images/Controls/{game.Platform}/{controller}.png";
to send the path to an image on the wpf but I've come to leanr that I cannot pass this same variable into File.Exists
. So I had to come up with an alternative.
I've discovered this:
string exists = Assembly.GetEntryAssembly().Location;
which gets me this result: F:\FrontEnd\FrontEnd.exe
So my question is, how do I modify the var exists
so that is matches the structure of var path
?
Upvotes: 0
Views: 970
Reputation: 2766
Try this
var path = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
var pathtoCombine = Path.Combine(path, "themes\\custom\\images\\controls\\pc");
var image = Path.Combine(pathtoCombine, "image.txt");
var exists = File.Exists(image);
You can change "themes\\custom\\images\\controls\\pc"
to $"themes\\custom\\images\\controls\\{platform}\\{imagename}"
This gives the root of your assembly. for example
c:\users\\documents\visual studio 2015\Projects\StringSandbox\StringSandbox\bin\Debug
Upvotes: 0
Reputation: 17001
You can use the different members of System.IO.Path
to manipulate the path strings.
var exePath = @"F:\FrontEnd\FrontEnd.exe";
var filePath = @"/Themes/Custom/Images/Controls/{game.Platform}/{controller}.png";
var relPath = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(exePath),filePath);
var packPath = @"pack://siteoforigin:,,," + relPath;
Upvotes: 1