Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
211 views
Welcome To Ask or Share your Answers For Others

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...