如何设置C#项目中,以图片目录的相对路径? [英] How to set relative path to Images directory inside C# project?
问题描述
我工作在C#项目,我需要使用相对路径来获得Images目录中的图像。我曾尝试
I am working on C# project i need to get the images from Images directory using relative path. I have tried
var path = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + @"\Images\logo.png";
var logoImage =new LinkedResource(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)+@"\Images\logo.png")
{
但这些...
没有运气在程序运行时,我已经被复制到输出目录中的图像,但它不T皮卡的图像。
But no luck with these... I have made the images to be copied to output directory when the program is running but it doesn't pickup those images.
推荐答案
如果您在C#中使用LinkedResource()是最有可能不是以皮卡的相对URI或文件位置。
If you are using LinkedResource() in C# it is most likely not to pickup your relative URI or the file location.
您可以使用一些额外的一段代码
You can use some extra piece of code
var outPutDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
var logoimage = Path.Combine(outPutDirectory, "Images\\logo.png");
string relLogo = new Uri(logoimage).LocalPath;
var logoImage = new LinkedResource(relLogo)
现在它会皮卡你相对路径,将其转换为绝对路径在内存中,它会帮助你得到的图像。
Now it will pickup your relative path, convert this to absolute path in memory and it will help you get the images.
这篇关于如何设置C#项目中,以图片目录的相对路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!