/// <summary> /// Embeds a single resource into the assembly. /// </summary> void AddResource(ModuleBuilder moduleBuilder, ResourceFile resourceFile) { string fileName = resourceFile.FileName; IResourceWriter resourceWriter = moduleBuilder.DefineResource(Path.GetFileName(fileName), resourceFile.Name, ResourceAttributes.Public); string extension = Path.GetExtension(fileName).ToLowerInvariant(); if (extension == ".resources") { ResourceReader resourceReader = new ResourceReader(fileName); using (resourceReader) { IDictionaryEnumerator enumerator = resourceReader.GetEnumerator(); while (enumerator.MoveNext()) { string key = enumerator.Key as string; Stream resourceStream = enumerator.Value as Stream; if (resourceStream != null) { BinaryReader reader = new BinaryReader(resourceStream); MemoryStream stream = new MemoryStream(); byte[] bytes = reader.ReadBytes((int)resourceStream.Length); stream.Write(bytes, 0, bytes.Length); resourceWriter.AddResource(key, stream); } else { resourceWriter.AddResource(key, enumerator.Value); } } } } else { resourceWriter.AddResource(resourceFile.Name, File.ReadAllBytes(fileName)); } }
/// <summary>Enbdeds resource into assembly</summary> /// <param name="builder"><see cref="ModuleBuilder"/> to embede resource in</param> /// <param name="name">Name of the resource</param> /// <param name="path">File to obtain resource from</param> /// <param name="attributes">Defines resource visibility</param> //DefineResource // Exceptions: // System.ArgumentException: // name has been previously defined or if there is another file in the assembly // named fileName.-or- The length of name is zero.-or- The length of fileName // is zero.-or- fileName includes a path. // // System.ArgumentNullException: // name or fileName is null. // // System.Security.SecurityException: // The caller does not have the required permission. //ResourceReader // Exceptions: // System.ArgumentException: // The stream is not readable. // // System.ArgumentNullException: // The stream parameter is null. // // System.IO.IOException: // An I/O error has occurred while accessing stream. //AddResource // Exceptions: // System.ArgumentNullException: // The name parameter is null. //ReadAllBytes // Exceptions: // System.ArgumentException: // path is a zero-length string, contains only white space, or contains one // or more invalid characters as defined by System.IO.Path.InvalidPathChars. // // System.ArgumentNullException: // path is null. // // System.IO.PathTooLongException: // The specified path, file name, or both exceed the system-defined maximum // length. For example, on Windows-based platforms, paths must be less than // 248 characters, and file names must be less than 260 characters. // // System.IO.DirectoryNotFoundException: // The specified path is invalid (for example, it is on an unmapped drive). // // System.IO.IOException: // An I/O error occurred while opening the file. // // System.UnauthorizedAccessException: // path specified a file that is read-only.-or- This operation is not supported // on the current platform.-or- path specified a directory.-or- The caller does // not have the required permission. // // System.IO.FileNotFoundException: // The file specified in path was not found. // // System.NotSupportedException: // path is in an invalid format. // // System.Security.SecurityException: // The caller does not have the required permission. private void AddResourceFile(ModuleBuilder builder,string name, FullPath path, ResourceAttributes attributes) { IResourceWriter rw = builder.DefineResource(path.FileName, name, attributes); string ext = path.Extension.ToLower(); if(ext == ".resources") { ResourceReader rr = new ResourceReader(path); using(rr) { System.Collections.IDictionaryEnumerator de = rr.GetEnumerator(); while(de.MoveNext()) { string key = de.Key as string; rw.AddResource(key, de.Value); } } } else { rw.AddResource(name, File.ReadAllBytes(path)); } }
/// <summary> /// Embeds a single resource into the assembly. /// </summary> void AddResource(ModuleBuilder moduleBuilder, ResourceFile resourceFile) { string fileName = resourceFile.FileName; string extension = Path.GetExtension(fileName).ToLowerInvariant(); if (extension == ".resources") { string fullFileName = Path.GetFileName(fileName); IResourceWriter resourceWriter = moduleBuilder.DefineResource(fullFileName, resourceFile.Name, ResourceAttributes.Public); AddResources(resourceWriter, fileName); } else { moduleBuilder.DefineManifestResource(resourceFile.Name, new FileStream(fileName, FileMode.Open), ResourceAttributes.Public); } }