How do I get the resource name from the resource o

2019-01-19 13:33发布

Say I have an exe added into my resources folder. Now how can I get the name (or even the fullpath from which its linked so that I can still have the file name) of the resource as string?

From Properties.Resources.myApp how do I get the string "myApp". ToString() doesnt work. If it is important to embed the file to get the name, I can.

Edit: My question is not specifically to get the name of exe resource. But that one generic approach which gives me the name of the resource! For instance what if my resource is a bitmap image? I need to print "Lily" from Properties.Resources.Lily. How to achieve this? ToString wont work anyways.

5条回答
太酷不给撩
2楼-- · 2019-01-19 14:07

When you embed some file in your project resources it will be embed in your executable file when build your project so it's not exist separately from your project executable, it's not exists on your hard drive. so if you want this myApp.exe in your project folder put it in the project folder then go

  • visual studio >>
  • Solution explorer >>
  • Click Show all files in the upper left corner >>
  • MyApp.exe will benn visible >>
  • Right click myApp.exe click Include in project >>
  • Hit F4 so properties page is shown and focused >>
  • set Copy to output directory to "Copy always"

enter image description hereenter image description here

Now if you want the path of myApp.exe use :

System.IO.Path.Combine(Application.StartupPath, "myApp.exe");

Update You should hard code "myApp" because if you go to definition of :

Properties.Resources.myApp 

you will see :

internal static byte[] myApp {
        get {
            object obj = ResourceManager.GetObject("myApp", resourceCulture);
            return ((byte[])(obj));
        }
    }

It's hard coded !!!!

查看更多
何必那么认真
3楼-- · 2019-01-19 14:11

With System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceNames(); you get a list of all ressources in your project.

Now if you want to search for all exe files (I guess you have only one embedded) use the following snippet to get your assembly name.

   var ressourceList = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceNames();
   var filename = ressourceList.Where(x => x.EndsWith(".exe")).FirstOrDefault();

The string format is "YourProgram.YourAssemblyName.exe" so just remove the first part of that string and you have your embedded ressource filename.

Edit: Why don't you enumerate through your ressources and strip the leading namespace + trailing file extensions?

 // returns just the names
  public static IEnumerable<String> GetEmbeddedResourceNames()
        {
            var returnList = new List<String>();
            foreach (var res in Assembly.GetExecutingAssembly().GetManifestResourceNames())
            {
                var s = Assembly.GetExecutingAssembly().GetName();
                returnList.Add(Regex.Replace(res.Replace(s.Name + ".", ""), @"\.[^.]*$", ""));
            }
            return returnList;
        }

Edit:

To get a ressource by name, use var prop = Properties.Resources.ResourceManager.GetObject("YourRessourceNameWithoutExtension");

查看更多
疯言疯语
4楼-- · 2019-01-19 14:13

It's quite easy using Linq Expressions:

using System.Linq.Expressions;
//...
static string GetNameOf<T>(Expression<Func<T>> property)
{
  return (property.Body as MemberExpression).Member.Name;
}
// Usage:
var s = GetNameOf(() => Properties.Resources.Lily);

s shoud be Lily

查看更多
贼婆χ
5楼-- · 2019-01-19 14:16

Do you mean you want to have the assembly? Here a little code snippet that get´s you all exe-files from a directory:

foreach (string fileName in Directory.GetFiles("./Files"))
{
    FileInfo fileInfo = new FileInfo(fileName);

    if (fileInfo.Extension.Equals(".exe"))
    {
        Assembly pluginAssembly = Assembly.LoadFrom(fileName);

        //...
    }
}
查看更多
孤傲高冷的网名
6楼-- · 2019-01-19 14:29

I know this is very old, but the accepted answer is no longer necessarily the best answer. As of C# 6.0 you can just use nameof(...):

string resourceName = nameof(Properties.Resources.MyResourceName);
// resourceName == "MyResourceName"

Much simpler!

查看更多
登录 后发表回答