---
title: "保存到桌面 功能"
author: "njmnjx"
date: "2013-11-19T15:34:32Z"
tags: []
categories: ["编程语言"]
slug: "untitled"
draft: false
lastmod: "2013-11-19T15:43:24Z"
id: 1976957
url: "https://njm.iteye.com/blog/1976957"
---

一、准备一个icon图标，图片类型必须是icon类型，不能通过bmp或者其它类型的图片改后缀获得。
icon图标可以通过一些在线生成图片的网站生成。

二、在网站的根目录下放置这个图标。可以在“保存到桌面”链接打开的页面head中放置如下内容

```css

<link rel="Bookmark" href="favicon.ico" /><!--或放置则此行比较重要-->
<link rel="icon" href="favicon.ico" type="image/x-icon" />
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />

```


三、功能页面 SaveToDeskTop.aspx

```C#

public partial class SaveToDeskTop : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                string url = Qlyx.Common.DNTRequest.GetString("tourl");
                string showName = Qlyx.Common.DNTRequest.GetString("title");
                string icon = Qlyx.Common.DNTRequest.GetString("icon");
                int iconIndex = Qlyx.Common.DNTRequest.GetInt("iconindex",0);

                if (string.IsNullOrEmpty(url))
                {
                    url = "http://www.***.com/";
                }
                if (string.IsNullOrEmpty(icon))
                {
                    icon = "http://www.***.com/favicon.ico";
                }
                if (string.IsNullOrEmpty(showName))
                {
                    showName = "***.url";
                }
                else {
                    showName = showName + ".url";
                }

                sDeskTop(url, showName,icon,iconIndex);
            }
        }

        private void sDeskTop(string url, string showName, string icon, int iconIndex)
        {
            string sb = "[internetshortcut]\r\n"
                    + "URL=" + url + "\r\n"
                    + "IconFile=" + icon + "\r\n"
                    + "Prop3=19,2\r\n"
                    + "IconIndex=" + iconIndex;

            Response.Clear();
            Response.Buffer = true;
            Response.Charset = "GB2312";

            string strBrowser = HttpContext.Current.Request.Browser.Browser;
            if (strBrowser.ToUpper() == "IE" || strBrowser.ToUpper() == "APPLEMAC-SAFARI")
            {
                Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(showName, System.Text.Encoding.UTF8));
            }
            else
            {
                Response.AppendHeader("Content-Disposition", "attachment;filename=" + showName);
            }

            Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            Response.ContentType = "application/octet-stream";
            EnableViewState = false;
            Response.Write(sb.ToString());
            Response.End();
        }
    }

```

<span style="color:red">**四、不得不提：下载保存到桌面后双击打开这个文件，则文件的图标自动切换成上面的icon图标，只需一次。**</span>