将文件添加到属性后尝试查找文件的路径效果不佳。在我的属性中,该文件如下所示:
internal static System.Drawing.Bitmap one {
get {
object obj = ResourceManager.GetObject("one", resourceCulture);
return ((System.Drawing.Bitmap)(obj));
}
}
如何找到该文件的路径? 然后我尝试像这样使用它:
System.Reflection.Assembly thisExe;
thisExe = System.Reflection.Assembly.GetExecutingAssembly();
System.IO.Stream file =
thisExe.GetManifestResourceStream("WindowsFormsApplication1.Properties.Resources.one");
this.pictureBox1.Image = Image.FromStream(file);
当我运行此代码时:
System.Reflection.Assembly thisExe;
thisExe = System.Reflection.Assembly.GetExecutingAssembly();
string [] resources = thisExe.GetManifestResourceNames();
string list = "";
// Build the string of resources.
foreach (string resource in resources)
list += resource + "\r\n";
它为我提供了以下路径:“WFA1.Form1.resources”和“WFA1.Properties.Resources.resources”
可以添加我的资源已嵌入。
如果您需要更多信息,请告诉我。
所以我想要的是我的文件的路径,或者有关如何找到该路径的信息。环顾四周后,他们说这应该可行:
System.IO.Stream file =
thisExe.GetManifestResourceStream("[WindowsFormsApplication1.Form1.resources].[a.jpg]");
IE:
System.IO.Stream file =
thisExe.GetManifestResourceStream("[Namespace].[file and extension]");
所以看来我的命名空间是错误的,因为它仍然在这一行返回 null:
this.pictureBox1.Image = Image.FromStream(file);
最佳答案
我已经看过这个,我不相信有一种方法可以在不进行一些认真的字符串构建/操作的情况下获取路径。这当然可能会导致更大的问题。
我认为这段代码是不够的:
var bmp = new Bitmap(WindowsFormsApplication1.Properties.Resources.one);
pictureBox1.Image = bmp;
而不是这个:
var thisExe = Assembly.GetExecutingAssembly();
var file = thisExe.GetManifestResourceStream("WindowsFormsApplication1.Properties.Resources.one");
if (file != null) pictureBox1.Image = Image.FromStream(file);
选项 2:
var assembly = System.Reflection.Assembly.GetExecutingAssembly();
var stream = assembly.GetManifestResourceStream("WindowsFormsApplication1.Properties.Resources.one.jpg");
var tempPath = Path.GetTempPath();
File.Save(stream, tempPath);
关于c# - 将文件添加到属性后查找文件路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21896192/