SimplyZ
SimplyZ

Reputation: 908

What is this code doing?

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.

Upvotes: 5

Views: 740

Answers (3)

ChrisLively
ChrisLively

Reputation: 88044

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.

Upvotes: 4

Josh M.
Josh M.

Reputation: 27773

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.

Upvotes: 1

Aliostad
Aliostad

Reputation: 81660

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.

Upvotes: 4

Related Questions