欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net修改注册表(页面打印时页眉、页脚、页边距、背景颜色和图像)

程序员文章站 2023-01-30 23:52:28
using System; using System.Collections.Generic; using System.Web; using System.Web.UI;...

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Win32;

public partial class PrintSetDefault : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (ReadReg() == 1)
        {
            if (WriteReg() == 1)
            {
                ReadReg();
                Response.Write("Game over!");
            }
        }
    }

 

    /// <summary>
    /// 读注册表
    /// </summary>
    /// <returns></returns>
    protected int ReadReg()
    {
       //读注册表
        RegistryKey regRead;
       //读取HKEY_CURRENT_USER主键里的子键
        regRead = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Internet Explorer\\PageSetup", true);
       if (regRead == null) //如果该子键不存在
        {
            Response.Write("PageSetup:null</br>");
            return 0;
        }
        else
        {
            Response.Write("PageSetup:OK</br>");

            object obj = regRead.GetValue("header") == null ? "null" : regRead.GetValue("header"); //读取值
            Response.Write("header:" + obj.ToString() + "</br>");

            obj = regRead.GetValue("footer") == null ? "null" : regRead.GetValue("footer"); //读取值
            Response.Write("footer:" + obj.ToString() + "</br>");

            obj = regRead.GetValue("margin_left") == null ? "null" : regRead.GetValue("margin_left"); //读取值
            Response.Write("margin_left:" + obj.ToString() + "</br>");

            obj = regRead.GetValue("margin_right") == null ? "null" : regRead.GetValue("margin_right"); //读取值
            Response.Write("margin_right:" + obj.ToString() + "</br>");

            obj = regRead.GetValue("margin_top") == null ? "null" : regRead.GetValue("margin_top"); //读取值
            Response.Write("margin_top:" + obj.ToString() + "</br>");

            obj = regRead.GetValue("margin_bottom") == null ? "null" : regRead.GetValue("margin_bottom"); //读取值
            Response.Write("margin_bottom:" + obj.ToString() + "</br>");

            obj = regRead.GetValue("Print_Background") == null ? "null" : regRead.GetValue("Print_Background"); //读取值
            Response.Write("Print_Background:" + obj.ToString() + "</br>");

            Response.Write("</br>");
        }
        regRead.Close();
        return 1;
    }

    /// <summary>
    /// 写注册表
    /// </summary>
    protected int WriteReg()
    {
        //写注册表
         RegistryKey regWrite;
        //往HKEY_CURRENT_USER主键里的写子键
         //如果子键已经存在系统会自动覆盖它
         regWrite = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Internet Explorer\\PageSetup");
        //往Test子键里添数据项
         regWrite.SetValue("header", "&w&bPage &p of &P");
        regWrite.SetValue("footer", "&u&b&d");
        regWrite.SetValue("margin_left", 0.750000);
        regWrite.SetValue("margin_right", 0.750000);
        regWrite.SetValue("margin_top", 0.750000);
        regWrite.SetValue("margin_bottom", 0.750000);
        regWrite.SetValue("Print_Background", "no");
        //关闭该对象
         regWrite.Close();

        return 1;

    }
}
 
以上结果是恢复成默认值。也可以改为0。

   Eat to live, but do not live to eat.
 

 

摘自 张银的博客