星期日, 1月 18, 2009

ClickOnce加入桌面Shortcut

ClickOnce本身沒支援
不過還是有人硬寫出來了

/// <summary>
/// This will create a Application Reference file on the users desktop
/// if they do not already have one when the program is loaded.
// If not debugging in visual studio check for Application Reference
// #if (!debug)
// CheckForShortcut();
// #endif
/// </summary>
void CheckForShortcut()
{
ApplicationDeployment ad = ApplicationDeployment.CurrentDeployment;
if (ad.IsFirstRun)
{
Assembly code = Assembly.GetExecutingAssembly();
string company = string.Empty;
string description = string.Empty;
if (Attribute.IsDefined(code, typeof(AssemblyCompanyAttribute)))
{
AssemblyCompanyAttribute ascompany = (AssemblyCompanyAttribute)Attribute.GetCustomAttribute(code,typeof(AssemblyCompanyAttribute));
company = ascompany.Company;

}
if (Attribute.IsDefined(code, typeof(AssemblyDescriptionAttribute)))
{
AssemblyDescriptionAttribute asdescription = (AssemblyDescriptionAttribute)Attribute.GetCustomAttribute(code, typeof(AssemblyDescriptionAttribute));
description = asdescription.Description;
}
if (company != string.Empty && description != string.Empty)
{
string desktopPath = string.Empty;
desktopPath = string.Concat(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "\\", description, ".appref-ms");
string shortcutName = string.Empty;
shortcutName = string.Concat(Environment.GetFolderPath(Environment.SpecialFolder.Programs), "\\", company, "\\", description, ".appref-ms");
System.IO.File.Copy(shortcutName, desktopPath, true);
}
}
}


remark
1.clickonce利用company name在「開始/程式集」中建立路徑,而description為「捷徑檔的名稱」
所以記得在clicknoce中設置company及description,否則上面的程式會找不到
2.另外clickonce的「發行/選項」的「產品名稱」及「應用程式/組件資訊」的「描述」,兩者要一致,不然會找不到
說明:
「發行/選項」的「產品名稱」是clickonce部署後,在程式集命名捷徑的命稱
「應用程式/組件資訊」的「描述」是上述程式asdescription.Description所取得的,所以這一項要跟產品名稱相同,才找的到clickonce設的捷徑

References:
How to add Desktop Shortcut to ClickOnce Deployment Application

沒有留言: