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

📄 utils.cs

📁 上传的控件
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Web;
using System.Text.RegularExpressions;

namespace WUSGControl.Web.Upload
{
    /// <summary>
    /// 静态工具类,大部分是静态方法
    /// </summary>
    public class Utils
    {

        #region Fields

        private static string version;
       
        #endregion

        #region Constructors

        static Utils()
        {
            
        }

        public Utils()
        {
        }

        #endregion

        /// <summary>
        /// return the current httpcontext object.
        /// </summary>
        /// <returns></returns>
        public static HttpContext GetContext()
        {
            
            HttpContext context = HttpContext.Current;
            //if (context == null)
            //{
            //    throw new Exception("HttpContext not found");
            //}
           
            return context;
        }

        /// <summary>
        /// return the current version of assembly.
        /// </summary>
        /// <returns></returns>
        public static string GetVersion()
        {
            if (Utils.version == null)
            {
                int majorVersion = typeof(Utils).Assembly.GetName().Version.Major;
                Utils.version = majorVersion.ToString();
            }
            
            return Utils.version;
        }

        /// <summary>
        /// Return the path of upload folder.
        /// 返回上传文件夹路径,如果没有则返回系统的临时文件路径.
        /// </summary>
        /// <returns></returns>
        public static string GetUploadFolder()
        {
            //从隐藏字段中读取
            string uploadFolder = GetContext().Request["Sunrise_Web_Upload_UploadFolder"];

            //If upload folder deos not exist, use system temporary folder to hold the file.
            if ((uploadFolder == null) || (uploadFolder == string.Empty))
            {
                uploadFolder = Path.GetTempPath();
            }

            return uploadFolder;
        }
      

        /// <summary>
        /// Return true if client browser > IE 5.5
        /// </summary>
        /// <returns></returns>
        public static bool IsAccordantBrowser()
        {
            HttpBrowserCapabilities bc = GetContext().Request.Browser;

            if (bc.Browser != "IE" || float.Parse(bc.Version) < 5.5)
            {
                return false;
            }

            return true;
        }

        /// <summary>
        /// 根据新旧名字获取组合新名字,以免重复
        /// </summary>
        /// <param name="OFileName"></param>
        /// <param name="NFileName"></param>
        /// <returns></returns>
        public static string PNewFileName(string OFileName, string NFileName)
        {
            string NewFileName = "";
            string ExtendName = Path.GetExtension(OFileName);
            if (ExtendName != "")
            {
                OFileName = OFileName.Substring(0, OFileName.Length - ExtendName.Length);
                NFileName = NFileName.Substring(0, NFileName.Length - ExtendName.Length);
                NewFileName = OFileName + NFileName  + ExtendName;
            }
            else
            {
                NewFileName = OFileName + NFileName;
            }

            //替换空格
            NewFileName = Regex.Replace(NewFileName, @"\s", "");

            return NewFileName;
            //string NewFileName = Path.GetFileName(OFileName).Substring(0,OFileName.Length-OFileName.IndexOf("."))
        }

        /// <summary>
        /// Turn file size into a readability format.
        /// 最后的格式全部生成xml结点返回给客户端,
        /// 以便客户端可以读取最新的数据
        /// </summary>
        /// <param name="size"></param>
        /// <returns></returns>
        public static string GetFormatString(double size)
        {
            string sizeString;

            if (size >= 1048576)
            {
                sizeString = (Math.Round(size / 1048576, 2) + " m");
            }
            else if (size >= 1024)
            {
                sizeString = (Math.Round(size / 1024, 2) + " k");
            }
            else
            {
                sizeString = (size + " bytes");
            }

            return sizeString;
        }

        /// <summary>
        /// Turn time string into a readability format.
        /// </summary>
        /// <param name="span"></param>
        /// <returns></returns>
        public static string GetFormatString(TimeSpan span)
        {
            string timeString = string.Empty;
            if ((span.Days > 0) || (span.Hours > 0))
            {
                int hours = ((0x18 * span.Days) + span.Hours);
                timeString = (timeString + hours + "&nbsp;小时&nbsp;");
            }
            if (span.Minutes > 0)
            {
                timeString = (timeString + span.Minutes + "&nbsp;分&nbsp;");
            }
            if (span.Seconds > 0)
            {
                timeString = (timeString + span.Seconds + "&nbsp;秒&nbsp;");
            }

            return timeString;
        }

        public static void writem(string message)
        {
            ProcessLog log = ProcessLog.getInstance();
            log.writemessage(message);
        }
    }

    
    /// <summary>
    /// 建一个全局的日志类,把所有的处理过程信息保存进一个文本文件中,便于程序跟踪
    /// </summary>
    internal class ProcessLog
    {
        private static ProcessLog _processlog;
        private StreamWriter _swriter;
        private string _LogPath = "Log\\proLog.txt";
        private string filename;

        public static ProcessLog getInstance()
        {
            if (_processlog == null)
            {
                _processlog = new ProcessLog();
            }

            return _processlog;


        }

        private ProcessLog()
        {
            //_swriter = new FileStream(Utils.GetContext().Request.ApplicationPath + "\\proLog.txt",System.IO.FileMode.OpenOrCreate);
            //_swriter = File.CreateText(@"C:\\proLog.txt");
            
            filename = Path.Combine(Utils.GetContext().Server.MapPath("."), _LogPath);

            if (File.Exists(filename))
            {
                File.Delete(filename);
            }

            _swriter = File.CreateText(filename);
            _swriter.Flush();
            _swriter.Close();
        }
        //向文件中写信息
        public void writemessage(string message)
        {
            _swriter = File.AppendText(filename);
            _swriter.WriteLine(message + "  " + DateTime.Now.ToLongTimeString());
            _swriter.Flush();
            _swriter.Close();

        }
        //清除文件内容
        public void clearcontent()
        {
            if (File.Exists(Utils.GetContext().Server.MapPath(_LogPath)))
            {
                File.Delete(Utils.GetContext().Server.MapPath(_LogPath));
            }

        }
    }

}

⌨️ 快捷键说明

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