Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is this code doing?

Tags:

c#

.net

Could someone please explain to me what the following lines of code do?

dynamic shellApplication = Activator.CreateInstance(Type.GetTypeFromProgID("Shell.Application"));

string path = System.IO.Path.GetDirectoryName(filePath);
string fileName = System.IO.Path.GetFileName(filePath);

dynamic directory = shellApplication.NameSpace(path);
dynamic link = directory.ParseName(fileName);

dynamic verbs = link.Verbs();

I've searched the msdn library, but couldn't really understand what it did.

This isn't the full code, but I undertand the rest, it is just this part that I'm struggling with.

like image 302
SimplyZ Avatar asked Apr 20 '11 21:04

SimplyZ


3 Answers

Looks like it is retrieving the shell actions that a particular program is associated with. For example Open, Print, Edit, etc.

Open regedit and navigate to HKEY_LOCAL_MACHINE\SOFTWARE\Classes\textfile

Expand it out and look at the Shell key. The code should be returning verbs similar to that.

like image 132
NotMe Avatar answered Oct 04 '22 15:10

NotMe


This creates "Shell.Application" COM object and then uses dynamic to call methods on it.

It gets all the verbs that can be called on a file.

This is basically scripting. See here and here for a sample.

like image 44
Aliostad Avatar answered Oct 04 '22 15:10

Aliostad


To expand on Aliostad's answer, the dynamic keyword in C# allows you to call members and methods on an unknown type. This means using a dynamic variable you won't get intellisense since the compiler has no clue what members or methods the variable actually has. This is all figured out at runtime.

Here is a good explanation.

like image 40
Josh M. Avatar answered Oct 04 '22 15:10

Josh M.