2014年5月22日 星期四

[C#] 取得執行檔 (*.exe) 的圖標 (.ico) 【Get executable file's icon】

筆者最近最常開發的功能即為獲取執行檔的圖標,在此紀錄兩種不同的做法。

方法一:使用 SHGetFileInfo

using System.Runtime.InteropServices;

[DllImport("shell32.dll", CharSet=CharSet.Auto)]
public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbFileInfo, uint uFlags);

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
public struct SHFILEINFO
{
    public IntPtr hIcon;
    public int iIcon;
    public uint dwAttributes;
    [MarshalAs(UnmanagedType.ByvalTStr, SizeConst = 260)]
    public string szDisplayName;
    [MarshalAs(UnmanagedType.ByvalTStr, SizeConst = 80)]
    public string szTypeName;
};

public const uint SHGFI_ICON = 0x100;
public const uint SHGFI_LARGEICON = 0x0; // Large icon
public const uint SHGFI_SMALLICON = 0x1; // Small icon

IntPtr hImgSmall;
IntPtr hImgLarge;
SHFILEINFO shinfo = new SHFILEINFO();

public System.Drawing.Icon GetExeFileIcon(string exeFilePath)
{
    // Use this to get small icon
    hImgSmall = SHGetFileInfo(exeFilePath, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), SHGFI_ICON |  SHGFI_SMALLICON);

    // Use this to get large icon
    hImgLarge = SHGetFileInfo(exeFilePath, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), SHGFI_ICON |  SHGFI_LARGEICON);

    System.Drawing.Icon myIcon = System.Drawing.Icon.FromHandle(shinfo.hIcon);

    return myIcon;
}


方法二:使用 Icon 類別 (System.Drawing)

using System.Drawing;

Icon myIcon = null;

public Icon GetExeFileIcon(string exeFilePath)
{
    myIcon = Icon.ExetractAssociatedIcon(exeFilePath);

    return myIcon;
}

2014年5月14日 星期三

[C#] Get Windows shortcut's target path

筆者最近在開發 In-house Windows application 的其中一項功能

──抓取使用者電腦中已安裝的應用程式的開發公司名稱 (Company Name)

遇到了一個問題:

「如果擷取的檔案為捷徑檔 (.lnk),那麼將無法獲得 Company Name;但是,如果擷取的檔案為執行檔 (.exe),將可獲得 Company Name」

經筆者觀察,執行檔的捷徑檔 (.lnk) 中的目標皆指向此捷徑檔相對應的執行檔路徑。因此,藉由抓取捷徑檔 (.lnk) 的目標並將之投入 FileVersionInfo 類別即可獲得此捷徑檔 (.lnk) 的 Company Name,請參考下圖:


範例程式碼如下:

首先,必須在 C# 專案中加入參考 (Reference) IWshRuntimeLibrary

using IWshRuntimeLibrary;

public string GetLNKTargetPath(string LNKFilePath)
{
    // Input LNKFilePath: Your LNK file's absolute path.
    WshShell shell = new WshShell();
    IWshSortcut shortcut =

        (IWshSortcut)shell.CreateShortcut(LNKFilePath);
    return shortcut.TargetPath; // Your LNK file's target path.
}

2014年5月7日 星期三

[C#] 新增、刪除、取得 Windows 系統登錄檔 (Registry)

  在 Windows programming 中,經常會利用系統登錄檔 (Registry) 儲存應用程式 (Application) 相關組態 (Configuration) 等資訊。以下文章將示範如何使用 C# 進行系統登錄檔 (Registry) 的新增、刪除和修改。

在 C# 中對系統登錄檔 (Registry) 操作必須先載入相關 namespace:

using Microsoft.Win32;

接下來將以 Current User 下的 SOFTWARE 為例,進行操作的講解。

新增
string registryPath = @"SOFTWARE"; //HKEY_CURRENT_USER\Software
//開啟 Software 子系統登錄檔
RegistryKey regKey = Registry.CurrentUser.OpenSubKey(registryPath);
regKey.SetValue(registryName, registryValue); //建立 Name/Value
regKey.close();


刪除
string registryPath = @"SOFTWARE"; //HKEY_CURRENT_USER\Software
//開啟 Software 子系統登錄檔
RegistryKey regKey = Registry.CurrentUser.OpenSubKey(registryPath);
regKey.DeleteValue(registryName); //刪除 registryName
regKey.close();


取得
string registryPath = @"SOFTWARE"; //HKEY_CURRENT_USER\Software
//開啟 Software 子系統登錄檔
RegistryKey regKey = Registry.CurrentUser.OpenSubKey(registryPath);
regKey.GetValue(registryName); //取得 registryName
regKey.close();




參考資料:MSDN