web页面常用方法及INI文件的读取方法

  public static class PageBase        {
            public static String MD5(String input)
            {
                System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
                byte[] bytes = System.Text.Encoding.UTF8.GetBytes(input);
                bytes = md5.ComputeHash(bytes);
                md5.Clear();

                string ret = "";
                for (int i = 0; i < bytes.Length; i++)
                {
                    ret += Convert.ToString(bytes[i], 16).PadLeft(2, '0');
                }

                return ret.PadLeft(32, '0');
            }

    /// <summary>
            /// 截取指定长度字符串
            /// </summary>
            /// <param keywords="s"></param>
            /// <param keywords="l"></param>
            /// <returns></returns>
            public static string getStr(string s, int l)
            {
                string temp = s;
                if (Regex.Replace(temp, "[\u4e00-\u9fa5]", "zz", RegexOptions.IgnoreCase).Length <= l)
                {
                    return temp;
                }
                for (int i = temp.Length; i >= 0; i--)
                {
                    temp = temp.Substring(0, i);
                    if (Regex.Replace(temp, "[\u4e00-\u9fa5]", "zz", RegexOptions.IgnoreCase).Length <= l - 3)
                    {
                        temp = temp + "...";
                        return temp;
                    }
                }

                return "";
            }

            /// <summary>
            /// 获取客户端IP
            /// </summary>
            /// <returns></returns>
            public static string GetClientIP()
            {
                string result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
                if (null == result || result == String.Empty)
                {
                    result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
                }

                if (null == result || result == String.Empty)
                {
                    result = HttpContext.Current.Request.UserHostAddress;
                }
                return result;
            }

            /// <summary>
            /// 获取网站根目录路径
            /// </summary>
            /// <returns></returns>
            public static string GetWebSiteRootUrl()
            {
                return HttpContext.Current.Request.Url.AbsoluteUri.Replace(HttpContext.Current.Request.AppRelativeCurrentExecutionFilePath.Replace("~/", string.Empty), string.Empty);
            }

            /// <summary>
            /// 发送电子邮件
            /// </summary>
            /// <param keywords="addressTo">接收地址</param>
            /// <param keywords="addressFrom">发送地址</param>
            /// <param keywords="strSubject">邮件标题</param>
            /// <param keywords="strBody">邮件内容</param>
            /// <param keywords="smtpServer">smtp服务器配置</param>
            /// <param keywords="smtpPort">smtp端口</param>
            /// <param keywords="password">gmail密码</param>
            /// <param keywords="message">提示信息</param>
            /// <returns></returns>
            public static bool SendEmail(string addressTo, string addressFrom, string strSubject, string strBody, string smtpServer, int smtpPort, string password, ref string message)
            {
                try
                {
                    using (MailMessage xMessage = new MailMessage(addressFrom, addressTo, strSubject, strBody))
                    {
                        SmtpClient xClient;
                        using (xClient = new SmtpClient(smtpServer, smtpPort))
                        {
                            xClient.Credentials = new NetworkCredential(addressFrom, password);
                            xClient.EnableSsl = false;
                            xClient.Send(xMessage);
                        }
                    }
                    return true;
                }
                catch (System.Exception ex)
                {
                    message = ex.Message;
                    throw ex;

                }
            }

            ///   <summary>
            ///   将指定字符串按指定长度进行剪切,
            ///   </summary>
            ///   <param   keywords= "oldStr "> 需要截断的字符串 </param>
            ///   <param   keywords= "maxLength "> 字符串的最大长度 </param>
            ///   <param   keywords= "endWith "> 超过长度的后缀 </param>
            ///   <returns> 如果超过长度,返回截断后的新字符串加上后缀,否则,返回原字符串 </returns>
            public static string StringTruncat(string oldStr, int maxLength, string endWith)
            {
                if (string.IsNullOrEmpty(oldStr))
                    //   throw   new   NullReferenceException( "原字符串不能为空 ");
                    return oldStr + endWith;
                if (maxLength < 1)
                    throw new Exception("返回的字符串长度必须大于[0] ");
                if (oldStr.Length > maxLength)
                {
                    string strTmp = oldStr.Substring(0, maxLength);
                    if (string.IsNullOrEmpty(endWith))
                        return strTmp;
                    else
                        return strTmp + endWith;
                }
                return oldStr;
            }


            /// <summary>
            /// 取掉标签
            /// </summary>
            /// <param keywords="beHtml">带标签的正文</param>
            /// <returns>无标签正文</returns>
            public static string ToContent(string beHtml)
            {
                string result = System.Text.RegularExpressions.Regex.Replace(beHtml, @"<[^>]*>", "");
                return result;
            }

        /// <summary>
        /// 页面弹框
        /// </summary>
        /// <param name="Msg">显示输出参数</param>
        public void Alert(string Msg)
        {
            string expression = Msg.Trim();
            if (expression != "")
            {
                expression = expression.Replace("'", "\'");
                expression = expression.Replace("\"", "\\\"");
                expression = expression.Replace(@"\", @"\\");
                //expression = expression.Replace("\n", "\\n");
                base.ClientScript.RegisterStartupScript(this.GetType(), "PageBaseAlert", string.Format("alert('{0}');", expression), true);
            }
        }

        /// <summary>
        /// 解决方案路径
        /// </summary>
        /// <returns></returns>
        protected string Path()
        {
            try
            {
                string[] path = System.AppDomain.CurrentDomain.BaseDirectory.Split('\\');
                StringBuilder strpath = new StringBuilder();
                for (int i = 0; i < path.Length - 2; i++)
                {
                    strpath.Append(path[i] + "\\");
                }
                strpath.Remove(strpath.Length - 1, 1);
                return strpath.ToString();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

}

  //读取INI文件

    internal static class WinAPI
    {
        /// <summary>
        /// 写入ini配置文件
        /// </summary>
        /// <param name="section">INI文件中的段落</param>
        /// <param name="key">INI文件中的关键字</param>
        /// <param name="val">INI文件中关键字对应的值</param>
        /// <param name="filePath">INI文件完整的路径和名称</param>
        /// <returns></returns>
        [DllImport("kernel32")]
        protected static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
        /// <summary>
        /// 读取ini配置文件
        /// </summary>
        /// <param name="section">INI文件中的段落</param>
        /// <param name="key">INI文件中的关键字</param>
        /// <param name="def">无法读取数据时的省缺值</param>
        /// <param name="retVal">INI文件中关键字对应的值</param>
        /// <param name="size">数值的大小</param>
        /// <param name="filePath">INI文件完整的路径和名称</param>
        /// <returns></returns>
        [DllImport("kernel32")]
        protected static extern long GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
    }

posted @ 2013-01-09 10:20  liyx0618  阅读(1292)  评论(0编辑  收藏  举报