■ 바로가기를 생성하는 방법을 보여준다.
▶ 바로가기 생성하기 예제 (C#)
1 2 3 4 5 6 7 8 9 10 11 |
using System; using System.IO; string filePath = "c:\\Voyager.exe"; string desktopDirectoryPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); string shortcutFilePath = Path.Combine(desktopDirectoryPath, "Voyager.lnk"); string iconFilePath = "c:\\Voyager.ico"; CreateShortcut(filePath, desktopDirectoryPath, iconFilePath); |
▶ 바로가기 생성하기 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
using System; using System.IO; using System.Runtime.InteropServices; /// <summary> /// 바로가기 생성하기 /// </summary> /// <param name="filePath">파일 경로</param> /// <param name="shortcutDirectoryPath">바로가기 디렉토리 경로</param> /// <param name="shortcutIconFilePath">바로가기 아이콘 파일 경로</param> /// <param name="shortcutDescription">바로가기 설명</param> public static void CreateShortcut ( string filePath, string shortcutDirectoryPath, string shortcutIconFilePath = null, string shortcutDescription = null ) { if(string.IsNullOrWhiteSpace(filePath) || !File.Exists(filePath)) { return; } if(string.IsNullOrWhiteSpace(shortcutDirectoryPath) || !Directory.Exists(shortcutDirectoryPath)) { return; } string shortcutFileName = string.Format(Path.GetFileNameWithoutExtension(filePath) + ".lnk"); string shortcutFilePath = Path.Combine(shortcutDirectoryPath, shortcutFileName); Type type = Type.GetTypeFromCLSID(new Guid("00021401-0000-0000-C000-000000000046"), true); IShellLinkW shellLinkW = Activator.CreateInstance(type) as IShellLinkW; shellLinkW.SetPath(filePath); string workingDirectory = Path.GetDirectoryName(filePath); shellLinkW.SetWorkingDirectory(workingDirectory); if(!string.IsNullOrWhiteSpace(shortcutIconFilePath) && File.Exists(shortcutIconFilePath) && shortcutIconFilePath.EndsWith(".ico")) { shellLinkW.SetIconLocation(shortcutIconFilePath, 0); } if(!string.IsNullOrWhiteSpace(shortcutDescription)) { shellLinkW.SetDescription(shortcutDescription); } (shellLinkW as IPersistFile).Save(shortcutFilePath, true); Marshal.ReleaseComObject(shellLinkW); } |
▶ FINDATA.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
using System.Runtime.InteropServices; /// <summary> /// FINDDATA /// </summary> [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] public struct FINDDATA { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Public /// <summary> /// 파일 속성 /// </summary> public uint FileAttributes; /// <summary> /// 생성 일시 /// </summary> public System.Runtime.InteropServices.ComTypes.FILETIME CreationTime; /// <summary> /// 최종 액세스 일시 /// </summary> public System.Runtime.InteropServices.ComTypes.FILETIME LastAccessTime; /// <summary> /// 최종 작성 일시 /// </summary> public System.Runtime.InteropServices.ComTypes.FILETIME LastWriteTime; /// <summary> /// 상위 워드 파일 크기 /// </summary> public uint HighWordFileSize; /// <summary> /// 하위 워드 파일 크기 /// </summary> public uint LowWordFileSize; /// <summary> /// 예약 워드 0 /// </summary> public uint ReservedWord0; /// <summary> /// 예약 워드 1 /// </summary> public uint ReservedWord1; /// <summary> /// 파일명 /// </summary> [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)] public string FileName; /// <summary> /// 대체 파일명 /// </summary> [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 14)] public string AlternateFileName; } |
▶ IPersist.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
using System; using System.Runtime.InteropServices; /// <summary> /// IPersist /// </summary> [ComImport, Guid("0000010c-0000-0000-c000-000000000046"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] public interface IPersist { //////////////////////////////////////////////////////////////////////////////////////////////////// Method #region 클래스 ID 구하기 - GetClassID(classGuid) /// <summary> /// 클래스 ID 구하기 /// </summary> /// <param name="classGuid">클래스 GUID</param> [PreserveSig] void GetClassID(out Guid classGuid); #endregion } |
▶ IPersistFile.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
using System; using System.Runtime.InteropServices; /// <summary> /// IPersistFile /// </summary> [ComImport, Guid("0000010b-0000-0000-C000-000000000046"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] public interface IPersistFile : IPersist { //////////////////////////////////////////////////////////////////////////////////////////////////// Method #region 클래스 ID 구하기 - GetClassID(classGuid) /// <summary> /// 클래스 ID 구하기 /// </summary> /// <param name="classGuid">클래스 GUID</param> new void GetClassID(out Guid classGuid); #endregion #region 더티 여부 조사하기 - IsDirty() /// <summary> /// 더티 여부 조사하기 /// </summary> /// <returns>더티 여부</returns> [PreserveSig] int IsDirty(); #endregion #region 로드하기 - Load(filePath, mode) /// <summary> /// 로드하기 /// </summary> /// <param name="filePath">파일 경로</param> /// <param name="mode">모드</param> [PreserveSig] void Load([In, MarshalAs(UnmanagedType.LPWStr)] string filePath, uint mode); #endregion #region 저장하기 - Save(filePath, remember) /// <summary> /// 저장하기 /// </summary> /// <param name="filePath">파일 경로</param> /// <param name="remember">기억 여부</param> [PreserveSig] void Save ( [In, MarshalAs(UnmanagedType.LPWStr)] string filePath, [In, MarshalAs(UnmanagedType.Bool )] bool remember ); #endregion #region 저장 완료시 처리하기 - SaveCompleted(filePath) /// <summary> /// 저장 완료시 처리하기 /// </summary> /// <param name="filePath">파일 경로</param> [PreserveSig] void SaveCompleted([In, MarshalAs(UnmanagedType.LPWStr)] string filePath); #endregion #region 현재 파일 구하기 - GetCurFile(filePath) /// <summary> /// 현재 파일 구하기 /// </summary> /// <param name="filePath">파일 경로</param> [PreserveSig] void GetCurFile([In, MarshalAs(UnmanagedType.LPWStr)] string filePath); #endregion } |
▶ IShellLinkW.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
using System; using System.Text; using System.Runtime.InteropServices; /// <summary> /// IShellLinkW /// </summary> [ComImport(), InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("000214F9-0000-0000-C000-000000000046")] public interface IShellLinkW { //////////////////////////////////////////////////////////////////////////////////////////////////// Method #region 경로 구하기 - GetPath(filePathStringBuilder, maximumFilePathLength, findData, slgType) /// <summary> /// 경로 구하기 /// </summary> /// <param name="filePathStringBuilder">파일 경로 StringBuilder</param> /// <param name="maximumFilePathLength">최대 파일 경로 길이</param> /// <param name="findData">FINDDATA 객체</param> /// <param name="slgType">SLGP 종류</param> void GetPath ( [Out(), MarshalAs(UnmanagedType.LPWStr)] StringBuilder filePathStringBuilder, int maximumFilePathLength, out FINDDATA findData, SLGPType slgType ); #endregion #region ID 리스트 구하기 - GetIDList(itemIdentifierListHandle) /// <summary> /// ID 리스트 구하기 /// </summary> /// <param name="itemIdentifierListHandle">항목 식별자 목록 핸들</param> void GetIDList(out IntPtr itemIdentifierListHandle); #endregion #region ID 리스트 설정하기 - SetIDList(itemIdentifierListHandle) /// <summary> /// ID 리스트 설정하기 /// </summary> /// <param name="itemIdentifierListHandle">항목 식별자 목록 핸들</param> void SetIDList(IntPtr itemIdentifierListHandle); #endregion #region 설명 구하기 - GetDescription(descriptionStringBuilder, maximumDescriptionLength) /// <summary> /// 설명 구하기 /// </summary> /// <param name="descriptionStringBuilder">설명 StringBuilder</param> /// <param name="maximumDescriptionLength">최대 설명 길이</param> void GetDescription ( [Out(), MarshalAs(UnmanagedType.LPWStr)] StringBuilder descriptionStringBuilder, int maximumDescriptionLength ); #endregion #region 설명 설정하기 - SetDescription(description) /// <summary> /// 설명 설정하기 /// </summary> /// <param name="description">설명</param> void SetDescription([MarshalAs(UnmanagedType.LPWStr)] string description); #endregion #region 작업 디렉토리 구하기 - GetWorkingDirectory(workingDirectoryStringBuilder, maximumWorkingDirectoryLength) /// <summary> /// 작업 디렉토리 구하기 /// </summary> /// <param name="workingDirectoryStringBuilder">작업 디렉토리 StringBuilder</param> /// <param name="maximumWorkingDirectoryLength">최대 작업 디렉토리 길이</param> void GetWorkingDirectory ( [Out(), MarshalAs(UnmanagedType.LPWStr)] StringBuilder workingDirectoryStringBuilder, int maximumWorkingDirectoryLength ); #endregion #region 작업 디렉토리 설정하기 - SetWorkingDirectory(workingDirectory) /// <summary> /// 작업 디렉토리 설정하기 /// </summary> /// <param name="workingDirectory">작업 디렉토리</param> void SetWorkingDirectory([MarshalAs(UnmanagedType.LPWStr)] string workingDirectory); #endregion #region 인자 구하기 - GetArguments(argumentListStringBuilder, maximumArgumentListLength) /// <summary> /// 인자 구하기 /// </summary> /// <param name="argumentListStringBuilder">인자 리스트 StringBuilder</param> /// <param name="maximumArgumentListLength">최대 인자 목록 길이</param> void GetArguments ( [Out(), MarshalAs(UnmanagedType.LPWStr)] StringBuilder argumentListStringBuilder, int maximumArgumentListLength ); #endregion #region 인자 설정하기 - SetArguments(argumentList) /// <summary> /// 인자 설정하기 /// </summary> /// <param name="argumentList">인자 리스트</param> void SetArguments([MarshalAs(UnmanagedType.LPWStr)] string argumentList); #endregion #region 핫키 구하기 - GetHotkey(hotKey) /// <summary> /// 핫키 구하기 /// </summary> /// <param name="hotKey">핫키</param> void GetHotkey(out short hotKey); #endregion #region 핫키 설정하기 - SetHotkey(hotKey) /// <summary> /// 핫키 설정하기 /// </summary> /// <param name="hotKey">핫키</param> void SetHotkey(short hotKey); #endregion #region 보여주기 명령 구하기 - GetShowCmd(showCommand) /// <summary> /// 보여주기 명령 구하기 /// </summary> /// <param name="showCommand">보여주기 명령</param> void GetShowCmd(out int showCommand); #endregion #region 보여주기 명령 설정하기 - SetShowCmd(showCommand) /// <summary> /// 보여주기 명령 설정하기 /// </summary> /// <param name="showCommand">보여주기 명령</param> void SetShowCmd(int showCommand); #endregion #region 아이콘 위치 구하기 - GetIconLocation(iconFilePathStringBuilder, maximumIconFilePathLength, iconHandle) /// <summary> /// 아이콘 위치 구하기 /// </summary> /// <param name="iconFilePathStringBuilder">아이콘 파일 경로</param> /// <param name="maximumIconFilePathLength">최대 아이콘 파일 경로 길이</param> /// <param name="iconHandle">아이콘 핸들</param> void GetIconLocation ( [Out(), MarshalAs(UnmanagedType.LPWStr)] StringBuilder iconFilePathStringBuilder, int maximumIconFilePathLength, out int iconHandle ); #endregion #region 아이콘 위치 설정하기 - SetIconLocation(iconFilePath, iconHandle) /// <summary> /// 아이콘 위치 설정하기 /// </summary> /// <param name="iconFilePath">아이콘 파일 경로</param> /// <param name="iconHandle">아이콘 핸들</param> void SetIconLocation([MarshalAs(UnmanagedType.LPWStr)] string iconFilePath, int iconHandle); #endregion #region 상대적 경로 설정하기 - SetRelativePath(relativePath, reserved) /// <summary> /// 상대적 경로 설정하기 /// </summary> /// <param name="relativePath">상대적 경로</param> /// <param name="reserved">예약</param> void SetRelativePath([MarshalAs(UnmanagedType.LPWStr)] string relativePath, int reserved); #endregion #region 분해하기 - Resolve(windowHandle, slrType) /// <summary> /// 분해하기 /// </summary> /// <param name="windowHandle">윈도우 핸들</param> /// <param name="slrType">SLR 종류</param> void Resolve(IntPtr windowHandle, SLRType slrType); #endregion #region 파일 경로 설정하기 - SetPath(filePath) /// <summary> /// 파일 경로 설정하기 /// </summary> /// <param name="filePath">파일 경로</param> void SetPath([MarshalAs(UnmanagedType.LPWStr)] string filePath); #endregion } |
▶ SLGPType.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
using System; /// <summary> /// SLGP 타입 /// </summary> [Flags] public enum SLGPType { /// <summary> /// SHORT_PATH /// </summary> SHORT_PATH = 1, /// <summary> /// UNC_PRIORITY /// </summary> UNC_PRIORITY = 2, /// <summary> /// RAW_PATH /// </summary> RAW_PATH = 4 } |
▶ SLRType.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
using System; /// <summary> /// SLR 타입 /// </summary> [Flags()] public enum SLRType { /// <summary> /// NO_UI /// </summary> NO_UI = 0x1, /// <summary> /// ANY_MATCH /// </summary> ANY_MATCH = 0x2, /// <summary> /// UPDATE /// </summary> UPDATE = 0x4, /// <summary> /// NO_UPDATE /// </summary> NO_UPDATE = 0x8, /// <summary> /// NO_SEARCH /// </summary> NO_SEARCH = 0x10, /// <summary> /// NO_TRACK /// </summary> NO_TRACK = 0x20, /// <summary> /// NO_LINK_INFORMATION /// </summary> NO_LINK_INFORMATION = 0x40, /// <summary> /// INVOKE_MSI /// </summary> INVOKE_MSI = 0x80 } |