|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using System.Reflection; |
| 6 | +using System.IO; |
| 7 | + |
| 8 | +namespace Caffeinated { |
| 9 | + class ReflectedShell { |
| 10 | + private const BindingFlags PublicInstance = |
| 11 | + BindingFlags.Public | BindingFlags.Instance; |
| 12 | + |
| 13 | + private Type type; |
| 14 | + private object shell; |
| 15 | + |
| 16 | + public ReflectedShell() { |
| 17 | + this.type = Type.GetTypeFromProgID("WScript.Shell"); |
| 18 | + this.shell = Activator.CreateInstance(type); |
| 19 | + } |
| 20 | + |
| 21 | + public object CreateShortcut( |
| 22 | + string linkFileName, |
| 23 | + string targetPath, |
| 24 | + string workingDir = null |
| 25 | + |
| 26 | + ) { |
| 27 | + object shortcut = type.InvokeMember( |
| 28 | + "CreateShortcut", PublicInstance | BindingFlags.InvokeMethod, |
| 29 | + null, shell, new object[] { linkFileName } |
| 30 | + ); |
| 31 | + |
| 32 | + Type shortcutType = shortcut.GetType(); |
| 33 | + shortcutType.InvokeMember( |
| 34 | + "TargetPath", PublicInstance | BindingFlags.SetProperty, |
| 35 | + null, shortcut, new object[] { targetPath } |
| 36 | + ); |
| 37 | + |
| 38 | + if (workingDir != null) { |
| 39 | + shortcutType.InvokeMember( |
| 40 | + "WorkingDirectory", |
| 41 | + PublicInstance | BindingFlags.SetProperty, |
| 42 | + null, shortcut, new object[] { workingDir } |
| 43 | + ); |
| 44 | + } |
| 45 | + |
| 46 | + shortcutType.InvokeMember( |
| 47 | + "Save", PublicInstance | BindingFlags.InvokeMethod, |
| 48 | + null, shortcut, null |
| 49 | + ); |
| 50 | + |
| 51 | + return shortcut; |
| 52 | + } |
| 53 | + |
| 54 | + public string GetSpecialFolder(string item) { |
| 55 | + object specFolders = type.InvokeMember( |
| 56 | + "SpecialFolders", PublicInstance | BindingFlags.GetProperty, |
| 57 | + null, shell, null |
| 58 | + ); |
| 59 | + |
| 60 | + Type specFoldersType = specFolders.GetType(); |
| 61 | + object path = specFoldersType.InvokeMember( |
| 62 | + "Item", PublicInstance | BindingFlags.InvokeMethod, |
| 63 | + null, specFolders, new object[] { item } |
| 64 | + ); |
| 65 | + |
| 66 | + return path as string; |
| 67 | + } |
| 68 | + } |
| 69 | +} |
0 commit comments