日期:2014-05-18 浏览次数:20965 次
// 将 VS2005ImageLibrary 目录下的图片放入一个资源文件 string VS2005ImageLibraryDir = "D:\\VS2005ImageLibrary"; void btnResourceTest_Click(object sender, EventArgs e) { ResXResourceWriter writer = new ResXResourceWriter(Path.Combine(VS2005ImageLibraryDir, "VS2005ImageLibrary.resx")); DirectoryInfo di = new DirectoryInfo(VS2005ImageLibraryDir); writeDirectory(writer, di); writer.Close(); writer.Dispose(); } void writeDirectory(ResXResourceWriter writer, DirectoryInfo directory) { FileInfo[] files = directory.GetFiles(); if (files != null && files.Length > 0) writeFiles(writer, files); DirectoryInfo[] dirs = directory.GetDirectories(); if (dirs != null && dirs.Length > 0) foreach (DirectoryInfo di in dirs) { writeDirectory(writer, di); } } void writeFiles(ResXResourceWriter writer, FileInfo[] files) { Image bitmap = null; Icon icon = null; string resourceName = null; foreach (FileInfo fi in files) { resourceName = fi.FullName.Substring(3).Replace('\\', '.'); if (fi.Extension.EndsWith("gif", StringComparison.OrdinalIgnoreCase) || fi.Extension.EndsWith("bmp", StringComparison.OrdinalIgnoreCase) || fi.Extension.EndsWith("png", StringComparison.OrdinalIgnoreCase)) { bitmap = Bitmap.FromFile(fi.FullName); writer.AddResource(resourceName, bitmap); bitmap.Dispose(); icon = null; } if (fi.Extension.EndsWith("ico", StringComparison.OrdinalIgnoreCase)) { icon = new Icon(fi.FullName); writer.AddResource(resourceName, icon); icon.Dispose(); icon = null; } } } void button37_Click(object sender, EventArgs e) { ResXResourceReader reader = new ResXResourceReader(Path.Combine(VS2005ImageLibraryDir, "VS2005ImageLibrary.resx")); System.Text.StringBuilder sb = new StringBuilder(); foreach (DictionaryEntry d in reader) { sb.AppendFormat("name: {0}, type: {1}{2}", d.Key.ToString(), d.Value.GetType().FullName, "\n"); } reader.Close(); MessageBox.Show(sb.ToString()); }
------解决方案--------------------
.resx是以 XML 格式存储的,试试xml的读写方法。