c# 4.0 - Getting MetaData information from a file using C# -
c# 4.0 - Getting MetaData information from a file using C# -
i trying metadata file using shell. using mvc5 , windows 8 os. here below code.
code
public jsonresult ind(string file) { list<string> arrheaders = new list<string>(); string filename = path.getfilename(file); shell shell = new shellclass(); folder rfolder = shell.namespace(file); folderitem rfiles = rfolder.parsename(filename); (int = 0; < short.maxvalue; i++) { string value = rfolder.getdetailsof(rfiles, i).trim(); arrheaders.add(value); } homecoming json(arrheaders, jsonrequestbehavior.allowget); }
when seek run above code, getting error unable cast com object of type 'shell32.shellclass' interface type 'shell32.ishelldispatch6'. operation failed because queryinterface phone call on com component interface iid '{286e6f1b-7113-4355-9562-96b7e9d64c54}' failed due next error: no such interface supported (exception hresult: 0x80004002 (e_nointerface)).
is there other improve solution read metadata different file formats?? pls suggest me anything. ll appreciate you.
thanks
seems problem in strict reference specific version of shell32 object(s) on build scheme differs target system.
here how reference looks in project file (c# project in given case):
<comreference include="shell32"> <guid>{50a7e9b0-70ef-11d1-b75a-00a0c90564fe}</guid> <versionmajor>1</versionmajor> <versionminor>0</versionminor> <lcid>0</lcid> <wrappertool>tlbimp</wrappertool> <isolated>false</isolated> </comreference>
so after referencing you're using specific version of shell (shellclass), implements
[guid("d8f015c0-c278-11ce-a49e-444553540000")] [typelibtype(4176)] public interface ishelldispatch
to prevent on different platforms improve create needed objects name using reflection, rather referencing specific types specific versions\libraries. instead of code:
shell shell = new shellclass(); folder rfolder = shell.namespace(file);
you can create folder object way:
private static folder getshell32namespacefolder(object folder) { var shellapptype = type.gettypefromprogid("shell.application"); var shell = activator.createinstance(shellapptype); homecoming (folder)shellapptype.invokemember("namespace", system.reflection.bindingflags.invokemethod, null, shell, new [] { folder }); }
also note method shell.namespace(...) parameter "the folder create folder object. can string specifies path of folder or 1 of shellspecialfolderconstants values." (msdn), should pass not file path, directory path of desired file by, example, path.getdirectoryname(file).
so code should work way:
public jsonresult ind(string file) { list<string> arrheaders = new list<string>(); string filename = path.getfilename(file); folder rfolder = getshell32namespacefolder(path.getdirectoryname(file)); folderitem rfiles = rfolder.parsename(filename); (int = 0; < short.maxvalue; i++) { string value = rfolder.getdetailsof(rfiles, i).trim(); arrheaders.add(value); } homecoming json(arrheaders, jsonrequestbehavior.allowget); } private static folder getshell32namespacefolder(object folder) { var shellapptype = type.gettypefromprogid("shell.application"); var shell = activator.createinstance(shellapptype); homecoming (folder)shellapptype.invokemember("namespace", system.reflection.bindingflags.invokemethod, null, shell, new [] { folder }); }
c#-4.0
Comments
Post a Comment