Get Relative Path to Linked Resource in C#

18,581

After compilation the resource files are part of a dll. So they do not exist anymore in the file system you distribute. If you need to have the resource files as a ressource then Hueso's solution is the only way of passing the files on to an external application.

However, if you don't mind the files being accessible by the user you can set the BuildAction to Content and set the Copy to Output Directory to either Copy if newer or Copy always. The files are then copied into the output directory and can be accessed from there. If the files are in sub folders these subfolders are also copied into the output directory.

Edit:

For the various build actions and their effect see:

Share:
18,581
JasCav
Author by

JasCav

Updated on June 04, 2022

Comments

  • JasCav
    JasCav almost 2 years

    I have a number of linked resources (text files in this case) in a C# project I am working on. I know the resources are linked via relative path in the Resources file. Is there any way to get that relative path from within the C# code?

    If there isn't, what I am trying to do is to pass that resources file to an external application as a commandline argument. If I can't do it the first way (which would obviously be the easiest) any other suggestions would be much appreciated. Thanks!

  • JasCav
    JasCav about 14 years
    @Obalix - I'm a bit confused. I thought the resources were only part of the DLL if I used Embedded Resources. I'm linking the resources. Doesn't this mean it'll be part of the file structure of the project upon release?
  • AxelEckenberger
    AxelEckenberger about 14 years
    Including a file into the solution does not mean that it gets copied to the output directory. This solely depends on the Copy to Output Directory of the file inside the solution.
  • AxelEckenberger
    AxelEckenberger about 14 years
    @Jason: I wasn't quite correct in the last comment, it depends on both properties (BuildAction and Copy to Output Directory) what will occur in the outut directory, and what will be previously done with the file (complie compiles it, embedded resource and resource includes it in a manifest, etc. - see link for more info).