c# - AppDomain.ResourceResolve event -
ms visual studio, c#.
i need locate localized resource files .\resource subdirectory. can't use probing xml element of config-file, i.e. project dll (it loaded in external application , located not in hosted application directory). try use appdomain.resourceresolve event problem...
now wrote "hello world" showing it:
using system; using system.collections.generic; using system.globalization; using system.io; using system.linq; using system.reflection; using system.resources; using system.text; using system.threading; namespace helloworld { class program { static void main(string[] args) { appdomain domain = appdomain.currentdomain; thread thread = thread.currentthread; thread.currentuiculture = new cultureinfo("en"); domain.resourceresolve += domain_resourceresolve; resourcemanager res = new resourcemanager(typeof(program)); console.writeline(res.getstring("message")); console.writeline("press key exit..."); console.readkey(); res.releaseallresources(); } static system.reflection.assembly domain_resourceresolve(object sender, resolveeventargs args) { assembly assembly = typeof(program).assembly; string name = path.combine(path.getdirectoryname(assembly.location), string.format("resources\\en\\{0}.resources.dll", path.getfilenamewithoutextension( assembly.location))); if (!file.exists(name)) { console.writeline("'{0}' file not found.", name); return null; } else { assembly result = assembly.loadfrom(name); if (result != null) console.writeline("'{0}' loaded.", name); return result; } } } }
the program.resx not exists i.e. if exists resourceresolve event not occur. exist program.en.resx , program.ru.resx files also. in properties of project set post-build event:
rmdir .\resources /s /q mkdir .\resources move .\en .\resources\en move .\ru .\resources\ru
my localized resource found , loaded successfully, exception (look screen)...
my "hello world" project attached also: sources.
if register event handler on appdomain.assemblyresolve
instead of appdomain.resourceresolve
works successful, appdomain.assemblyresolve
generate twice in case (i don't know why). decision found @josser - thank you. problem solved. if knows why appdomain.resourceresolve
don't working in case, , why appdomain.assemblyresolve
generate twice - grateful explanation.
Comments
Post a Comment