⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 customtheme.aspx.cs

📁 本程序基于 asp.net 2.0 +sql server 2
💻 CS
字号:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using lalablog;
public partial class user_customTheme : System.Web.UI.Page
{
    int userid;
    int themeid; 
    string themeName;
    string userThemePath;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userid"] != null)
        {
            this.userid = Convert.ToInt32(Session["userid"].ToString());
            this.userThemePath = Server.MapPath("~/app_themes");
            this.userThemePath += "\\userTheme" + this.userid;
        }
        else
            Response.Redirect("userLogin.aspx");

 
        if (Request.QueryString["themeid"] != null)
            this.themeid = Convert.ToInt32(Request.QueryString["themeid"]);

        if (!IsPostBack)
        {
                creatUserTheme();
                updateUserTheme(); 
            loadCss();
        }
       
    }


    private void updateUserTheme()
    {
        string sql = "update users set themeid=0 where userid=" + this.userid;

        openDb open1 = new openDb(sql);
        open1.update();
    }

    //用户使用系统提供的主题进行自定义的时候执行 
    private void creatUserTheme()
    {

        //检测用户自定义主题目录是否存在,如果存在,则删除目录后重建
        try
        {
       //  if (Directory.Exists(this.userThemePath))
         //     Directory.Delete(this.userThemePath,true );
          Directory.CreateDirectory(this.userThemePath);
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message+"aa");
        }
        finally
        { }
       
        //拷贝blog.css文件到用户目录
        theme theme1 = new theme(this.themeid);
        this.themeName = theme1.getName();
        string themePath = Server.MapPath("~/app_themes/"+themeName );

        try
        {
            if(Directory .Exists (themePath ))
            {
                DirectoryInfo di = new DirectoryInfo(themePath);

                FileInfo[] fi = di.GetFiles();

                foreach (FileInfo  filename in fi  )
                {
                    string src=filename .FullName.Trim ();
                    string des=this.userThemePath + "\\"+filename.Name;
                  File.Copy(src ,des.Trim () ,true  );
                }
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message+"bb");
        }
        finally
        { 
        
        }
        

    }
    private string uploadimg(string filename)
    {
        Boolean fileOK = false;

        string path = Server.MapPath("~/app_themes/usertheme"+this.userid +"/");
        filename = path + filename;

        //检验是否指定后缀
        if (FileUpload1.HasFile)
        {
            string fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
            String[] allowedExtensions =  { ".gif", ".png", ".jpeg", ".jpg" };
            for (int i = 0; i < allowedExtensions.Length; i++)
            {
                if (fileExtension == allowedExtensions[i])
                    fileOK = true;
            }
           int fileSize = FileUpload1.PostedFile.ContentLength;
            //利用postedFile.ContentLength来判断文件长度,检查是否符合文件大小,250000就是250k字节
            if (fileOK && fileSize < 1000000)
                fileOK = true;
            else
                fileOK = false;
        }

        if (fileOK)
        {
            try
            {
                FileUpload1.PostedFile.SaveAs(filename );
                Label2.Text = "图片成功上传";
            }
            catch (Exception ex)
            {
                myfunc m1 = new myfunc(ex.Message) ;
                Response.Write(m1.JavaMsg());
            }
        }
        else
        {
            myfunc m1=new myfunc ("无法上传的文件类型或文件大小超过允许值");
            Response.Write(m1.JavaMsg());
        }
        return "";
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        uploadimg("top.jpg");
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        string blogcss = Server.MapPath("~/app_themes/userTheme" +this.userid  + "/blog.css");

        try 
        {
            File.WriteAllText(blogcss, Server .HtmlEncode(tbBlogCss.Text));
            Label1.Text = "<br>成功保存css文件于" + blogcss;
        }
        catch (Exception ex)
        {
            Label1.Text = ex.Message;
        }
    }
    private void loadCss()
    {
        string blogcss = Server.MapPath("~/app_themes/userTheme" + this.userid + "/blog.css");
        try
        {
            tbBlogCss.Text = File.ReadAllText(blogcss);
            Label1.Text = "<br>成功读取css文件于" + blogcss;
        }
        catch (Exception ex)
        {
            Label1.Text = ex.Message;
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -