Home > database >  Get image file path from resources
Get image file path from resources

Time:12-03

I have an image file day.jpg in Resources folder and I want to access it in the code as string path not as byte[] img

Here's what I have tried.

string dayWallpaper = Assembly.GetExecutingAssembly().Location   @"..\..\Resources\day.jpg";
// Didn't found it
string dayWallpaper = Resource.day;
// Outputs byte[] and gives me an error

Then I tried to convert the byte[] to String didn't work as well

static byte[] SliceMe(byte[]? source, int pos)
{
   byte[]? destfoo = new byte[source.Length - pos];
   Array.Copy(source, pos, destfoo, 0, destfoo.Length);
   return destfoo;
}

static string ByteToPath(path)
{
   String file = Encoding.Unicode.GetString(SliceMe(path, 24)).TrimEnd("\0".ToCharArray());
   return file
}

Outputs black screen

Later I search for the file

if (File.Exists(dayWallpaper))
{
   do stuff
}
else
{
   Console.WriteLine("File does not exists");
}

And gives me the else statement.

CodePudding user response:

I managed to do it this way

string resourcePath = Path.GetFullPath(Assembly.GetExecutingAssembly().Location   @"\..\..\..\..\Resources");

string dayWallpaper = resourcePath   @"\day.jpg";

CodePudding user response:

In the answer you posted to your question, the fact that your relative path works is an "accident" that would fail on any other device deploying your app because without the existence of the source code project the path doesn't exist. One good option is to mark the day.jpg file as Copy to Output Directory at which point most installer bundlers will pick it up and deploy it in your setup.exe, msi etc. If you are specifically using the Visual Studio IDE, you would do it like this:

Copy if Newer option

Now, at runtime, to acquire the path to the copied file:

var srce = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Resources", "day.jpg");

However, there is more work to be done, because you state that you "want to store the image in a folder in the executable and the user could add more images later on." The present location of the file is not suitable for that purpose, so I would recommend the additional step of creating an AppData entry for the user to store their created content.

// Obtain a folder that "the user could add to later on".
var appData =     
    Path.Combine(
        Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
        typeof(Program).Assembly.GetName().Name
    );
Directory.CreateDirectory(appData);

Since you mention wanting to store the day.jpg image in that folder, go ahead and copy it to the AppData location (if not already there from a previous run of your app).

var dest = Path.Combine(appData, "day.jpg");

// Copy the image (if it's not there already) into folder that the user can add to.
if (!File.Exists(dest))
{
    File.Copy(
        sourceFileName: srce,
        destFileName: dest
    );
}

Alternatively, you could set the BuildAction to EmbeddedResource and manipulate the file as a byte stream and achieve the same end result.

  • Related