代码改变世界

vs clickonce 部署.net2.0依赖的项目 自动创建快捷方式

2011-04-12 16:20  kwjlk  阅读(613)  评论(0)    收藏  举报

解决办法有两个,

第一个、最简单的 将项目.net依赖升级到3.5 就可以在 项目属性>发布>选项>清单 里看到那个创建桌面快捷方式的选项了。但有些时候我们并不像升级.net依赖,所以才有

第二个办法

第二个方法:代码控制,老外告诉我们

  /// <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
        private static void CheckForShortcut()
        {
            if (ApplicationDeployment.IsNetworkDeployed)
            {
                ApplicationDeployment ad = ApplicationDeployment.CurrentDeployment;
                if (ad.IsFirstRun)
                {
                    Assembly code = Assembly.GetExecutingAssembly();
                    string company = string.Empty;
                    string product = string.Empty;

                    if (Attribute.IsDefined(code, typeof(AssemblyCompanyAttribute)))
                    {
                        AssemblyCompanyAttribute ascompany = (AssemblyCompanyAttribute)Attribute.GetCustomAttribute(code,
                            typeof(AssemblyCompanyAttribute));
                        company = ascompany.Company;
                    }

                    if (Attribute.IsDefined(code, typeof(AssemblyProductAttribute)))
                    {
                        AssemblyProductAttribute asproduct = (AssemblyProductAttribute)Attribute.GetCustomAttribute(code,
                            typeof(AssemblyProductAttribute));
                        product = asproduct.Product;
                    }
                    if (company != string.Empty && product != string.Empty)
                    {
                        string desktopPath = string.Empty;
                        desktopPath = string.Concat(Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
                            "\\", product, ".appref-ms");

                        string shortcutName = string.Empty;
                        shortcutName = string.Concat(Environment.GetFolderPath(Environment.SpecialFolder.Programs),
                            "\\", company, "\\", product, ".appref-ms");

                        System.IO.File.Copy(shortcutName, desktopPath, true);
                    }
                }
            }


        }

上面获取Assembly里的属性,有几个的,你可以看看自己需要用那个

(也许你们会遇到异常,那就是有一点没注意)

关键是 

if (ApplicationDeployment.IsNetworkDeployed) 
是不是clickonce 发布模式
 ApplicationDeployment ad = ApplicationDeployment.CurrentDeployment;
                if (ad.IsFirstRun) 
程序是第一次执行么(clickonce发布后,客户运行安装会自动启动安装后的程序的)?

string desktopPath = string.Empty; 
 desktopPath = string.Concat(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), 
                           "\\", product, ".appref-ms");
  string shortcutName = string.Empty; 
 shortcutName = string.Concat(Environment.GetFolderPath(Environment.SpecialFolder.Programs),  
                          "\\", company, "\\", product, ".appref-ms");
    System.IO.File.Copy(shortcutName, desktopPath, true);
创建快捷方式么
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(
false);
try
{
CheckForShortcut(); //加在这里
}
catch (Exception ex)
{

}
Application.Run(
new MainForm());
}

参考地址:http://geekswithblogs.net/murraybgordon/archive/2006/10/04/93203.aspx