自定义处理(得到浏览器,日期,本机的MAC地址)

#region  自定义处理

        
///   <summary>
        
///  获取 web.config 文件中指定 key 的值
        
///   </summary>
        
///   <param name="keyName"> key名称 </param>
        
///   <returns></returns>
         public   static   string  GetAppSettings( string  keyName)
        {
            
return  ConfigurationSettings.AppSettings[keyName];
        }


        
///   <summary>
        
///  按照指定格式输出时间
        
///   </summary>
        
///   <param name="NowDate"> 时间 </param>
        
///   <param name="type"> 输出类型 </param>
        
///   <returns></returns>
         public   static   string  WriteDate( string  NowDate,  int  type)
        {
            
double  TimeZone  =   0 ;
            DateTime NewDate 
=  DateTime.Parse(NowDate).AddHours(TimeZone);
            
string  strResult  =   "" ;

            
switch  (type)
            {
                
case   1 :
                    strResult 
=  NewDate.ToString();
                    
break ;
                
case   2 :
                    strResult 
=  NewDate.ToShortDateString().ToString();
                    
break ;
                
case   3 :
                    strResult 
=  NewDate.Year  +   " "   +  NewDate.Month  +   " "   +  NewDate.Day  +   " 日  "   +  NewDate.Hour  +   " "   +  NewDate.Minute  +   " "   +  NewDate.Second  +   " " ;
                    
break ;
                
case   4 :
                    strResult 
=  NewDate.Year  +   " "   +  NewDate.Month  +   " "   +  NewDate.Day  +   " " ;
                    
break ;
                
case   5 :
                    strResult 
=  NewDate.Year  +   " "   +  NewDate.Month  +   " "   +  NewDate.Day  +   " 日  "   +  NewDate.Hour  +   " "   +  NewDate.Minute  +   " " ;
                    
break ;
                
case   6 :
                    strResult 
=  NewDate.Year  +   " - "   +  NewDate.Month  +   " - "   +  NewDate.Day  +   "    "   +  NewDate.Hour  +   " : "   +  NewDate.Minute;
                    
break ;
                
default :
                    strResult 
=  NewDate.ToString();
                    
break ;
            }
            
return  strResult;
        }


        
private   static   int  Instr( string  strA,  string  strB)
        {
            
if string .Compare(strA, strA.Replace(strB, "" ))  >   0  )
            {
                
return   1 ;
            }
            
else
            {
                
return   0 ;
            }
        }


        
///   <summary>
        
///  判断客户端操作系统和浏览器的配置
        
///   </summary>
        
///   <param name="Info"> 客户端返回的头信息(Request.UserAgent) </param>
        
///   <param name="Type"> 获取类型:1为操作系统, 2为浏览器 </param>
        
///   <returns></returns>
         public   static   string  GetInfo( string  Info,  int  Type)
        {

            
string  GetInfo  =   "" ;
            
switch (Type)
            {
                
case   1 :
                    
if (Instr(Info, @" NT 5.1 " >   0 )
                    {
                        GetInfo 
=   " 操作系统:Windows XP " ;
                    }
                    
else   if (Instr(Info, @" Tel " >   0 )
                    {
                        GetInfo 
=   " 操作系统:Telport " ;
                    }
                    
else   if (Instr(Info, @" webzip " >   0 )
                    {
                        GetInfo 
=   " 操作系统:操作系统:webzip " ;
                    }
                    
else   if (Instr(Info, @" flashget " >   0 )
                    {
                        GetInfo 
=   " 操作系统:flashget " ;
                    }
                    
else   if (Instr(Info, @" offline " >   0 )
                    {
                        GetInfo 
=   " 操作系统:offline " ;
                    }
                    
else   if (Instr(Info, @" NT 5 " >   0 )
                    {
                        GetInfo 
=   " 操作系统:Windows 2000 " ;
                    }
                    
else   if (Instr(Info, @" NT 4 " >   0 )
                    {
                        GetInfo 
=   " 操作系统:Windows NT4 " ;
                    }
                    
else   if (Instr(Info, @" 98 " >   0 )
                    {
                        GetInfo 
=   " 操作系统:Windows 98 " ;
                    }
                    
else   if (Instr(Info, @" 95 " >   0 )
                    {
                        GetInfo 
=   " 操作系统:Windows 95 " ;
                    }
                    
else
                    {
                        GetInfo 
=   " 操作系统:未知 " ;
                    }
                    
break ;
                
case   2 :
                    
if (Instr(Info, @" NetCaptor 6.5.0 " >   0 )
                    {
                        GetInfo 
=   " 浏 览 器:NetCaptor 6.5.0 " ;
                    }
                    
else   if (Instr(Info, @" MyIe 3.1 " >   0 )
                    {
                        GetInfo 
=   " 浏 览 器:MyIe 3.1 " ;
                    }
                    
else   if (Instr(Info, @" NetCaptor 6.5.0RC1 " >   0 )
                    {
                        GetInfo 
=   " 浏 览 器:NetCaptor 6.5.0RC1 " ;
                    }
                    
else   if (Instr(Info, @" NetCaptor 6.5.PB1 " >   0 )
                    {
                        GetInfo 
=   " 浏 览 器:NetCaptor 6.5.PB1 " ;
                    }
                    
else   if (Instr(Info, @" MSIE 6.0b " >   0 )
                    {
                        GetInfo 
=   " 浏 览 器:Internet Explorer 6.0b " ;
                    }
                    
else   if (Instr(Info, @" MSIE 6.0 " >   0 )
                    {
                        GetInfo 
=   " 浏 览 器:Internet Explorer 6.0 " ;
                    }
                    
else   if (Instr(Info, @" MSIE 5.5 " >   0 )
                    {
                        GetInfo 
=   " 浏 览 器:Internet Explorer 5.5 " ;
                    }
                    
else   if (Instr(Info, @" MSIE 5.01 " >   0 )
                    {
                        GetInfo 
=   " 浏 览 器:Internet Explorer 5.01 " ;
                    }
                    
else   if (Instr(Info, @" MSIE 5.0 " >   0 )
                    {
                        GetInfo 
=   " 浏 览 器:Internet Explorer 5.0 " ;
                    }
                    
else   if (Instr(Info, @" MSIE 4.0 " >   0 )
                    {
                        GetInfo 
=   " 浏 览 器:Internet Explorer 4.0 " ;
                    }
                    
else
                    {
                        GetInfo 
=   " 浏 览 器:未知 " ;
                    }
                    
break ;
            }
            
return  GetInfo;
        }


        
///   <summary>
        
///  获取服务器本机的MAC地址
        
///   </summary>
        
///   <returns></returns>
         public   static   string  GetMAC_Address()
        {
            
string  strResult  =   "" ;

            ManagementObjectSearcher query                
=   new  ManagementObjectSearcher( " SELECT * FROM Win32_NetworkAdapterConfiguration " ) ;
            ManagementObjectCollection queryCollection    
=  query.Get();
            
foreach ( ManagementObject mo  in  queryCollection ) 
            { 
                
if (mo[ " IPEnabled " ].ToString()  ==   " True " ) strResult  =  mo[ " MacAddress " ].ToString();
            }

            
return  strResult;
        }



        
///   <summary>
        
///  转换文件路径中不规则字符
        
///   </summary>
        
///   <param name="path"></param>
        
///   <returns> string </returns>
         public   static   string  convertDirURL( string  path)
        {
            
return  AddLast(path.Replace( " / " , " / " ), " / " );
        }

        
///   <summary>
        
///  
        
///   </summary>
        
///   <param name="str"></param>
        
///   <returns> string </returns>
         public   static   string  convertXmlString( string  str)
        {
            
return   " <![CDATA[ "   +  str  +   " ]]> " ;
        }

        
///   <summary>
        
///  转换一个double型数字串为时间,起始 0 为 1970-01-01 08:00:00
        
///  原理就是,每过一秒就在这个数字串上累加一
        
///   </summary>
        
///   <param name="d"> double 型数字 </param>
        
///   <returns> DateTime </returns>
         public   static  DateTime ConvertIntDateTime( double  d)
        {
            DateTime time 
=  DateTime.MinValue;

            DateTime startTime 
=  DateTime.Parse( " 1970-01-01 08:00:00 " );

            time 
=  startTime.AddSeconds(d);

            
return  time;
        }

        
///   <summary>
        
///  转换时间为一个double型数字串,起始 0 为 1970-01-01 08:00:00
        
///  原理就是,每过一秒就在这个数字串上累加一
        
///   </summary>
        
///   <param name="time"> 时间 </param>
        
///   <returns> double </returns>
         public   static   double  ConvertDateTimeInt(DateTime time)
        {
            
double  intResult  =   0 ;

            DateTime startTime 
=  DateTime.Parse( " 1970-01-01 08:00:00 " );
            
            intResult 
=  (time  -  startTime).TotalSeconds;

            
return  intResult;
        }


        
///   <summary>
        
///  获取一个URL中引用的文件名称(包括后缀符)
        
///   </summary>
        
///   <param name="url"> URL地址 </param>
        
///   <returns> string </returns>
         public   static   string  GetFileName( string  url)
        {
            
// string[] Name = Function.SplitArray(url,'/');
            
// return Name[Name.Length - 1];
            
            
return  System.IO.Path.GetFileName(url);
        } 
        
        
///   <summary>
        
///  检测某一字符串的第一个字符是否与指定的
        
///  字符一致,否则在该字符串前加上这个字符
        
///   </summary>
        
///   <param name="Strings"> 字符串 </param>
        
///   <param name="Str"> 字符 </param>
        
///   <returns> 返回 string </returns>
         public   static   string  AddFirst( string  Strings,  string  Str)
        {
            
string  strResult  =   "" ;
            
if (Strings.StartsWith(Str))
            {
                strResult 
=  Strings;
            }
            
else
            {
                strResult 
=  String.Concat(Str, Strings);
            }
            
return  strResult;
        }


        
///   <summary>
        
///  检测某一字符串的最后一个字符是否与指定的
        
///  字符一致,否则在该字符串末尾加上这个字符
        
///   </summary>
        
///   <param name="Strings"> 字符串 </param>
        
///   <param name="Str"> 字符 </param>
        
///   <returns> 返回 string </returns>
         public   static   string  AddLast( string  Strings,  string  Str)
        {
            
string  strResult  =   "" ;
            
if (Strings.EndsWith(Str))
            {
                strResult 
=  Strings;
            }
            
else
            {
                strResult 
=  String.Concat(Strings, Str);
            }
            
return  strResult;
        }

        
///   <summary>
        
///  检测某一字符串的第一个字符是否与指定的
        
///  字符一致,相同则去掉这个字符
        
///   </summary>
        
///   <param name="Strings"> 字符串 </param>
        
///   <param name="Str"> 字符 </param>
        
///   <returns> 返回 string </returns>
         public   static   string  DelFirst( string  Strings,  string  Str)
        {
            
string  strResult  =   "" ;
            
if ( Strings.Length  ==   0  )  throw   new  Exception( " 原始字符串长度为零 " );

            
if (Strings.StartsWith(Str))
            {
                strResult 
=  Strings.Substring(Str.Length, Strings.Length  -   1 );
            }
            
else
            {
                strResult 
=  Strings;
            }

            
return  strResult;
        }

        
///   <summary>
        
///  检测某一字符串的最后一个字符是否与指定的
        
///  字符一致,相同则去掉这个字符
        
///   </summary>
        
///   <param name="Strings"> 字符串 </param>
        
///   <param name="Str"> 字符 </param>
        
///   <returns> 返回 string </returns>
         public   static   string  DelLast( string  Strings,  string  Str)
        {
            
string  strResult  =   "" ;

            
if (Strings.EndsWith(Str))
            {
                strResult 
=  Strings.Substring( 0 ,Strings.Length  -  Str.Length);
            }
            
else
            {
                strResult 
=  Strings;
            }

            
return  strResult;
        }

        
///   <summary>
        
///  获取一个目录的绝对路径(适用于WEB应用程序)
        
///   </summary>
        
///   <param name="folderPath"> 目录路径 </param>
        
///   <returns></returns>
         public   static   string  GetRealPath( string  folderPath)
        {
            
string  strResult  = "" ;

            
if ( folderPath.IndexOf( " :/ " >   0  )
            {
                strResult 
=  AddLast(folderPath,  " / " );
            }
            
else
            {
                
if ( folderPath.StartsWith( " ~/ " ) )
                {
                    strResult 
=  AddLast(System.Web.HttpContext.Current.Server.MapPath(folderPath),  " / " );
                }
                
else
                {
                    
string  webPath  =  System.Web.HttpContext.Current.Request.ApplicationPath  +   " / " ;
                    strResult 
=  AddLast(System.Web.HttpContext.Current.Server.MapPath(webPath  +  folderPath),  " / " );
                }
            }

            
return  strResult;
        }

        
///   <summary>
        
///  获取一个文件的绝对路径(适用于WEB应用程序)
        
///   </summary>
        
///   <param name="filePath"> 文件路径 </param>
        
///   <returns> string </returns>
         public   static   string  GetRealFile( string  filePath)
        {
            
string  strResult  = "" ;

            
// strResult = ((file.IndexOf(@":") > 0 || file.IndexOf(":/") > 0) ? file : System.Web.HttpContext.Current.Server.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath + "/" + file));
            strResult  =  ((filePath.IndexOf( " :/ " >   0  )  ?  
                filePath : 
                System.Web.HttpContext.Current.Server.MapPath(filePath));

            
return  strResult;
        }
        
///   <summary>
        
///  对字符串进行 HTML 编码操作
        
///   </summary>
        
///   <param name="str"> 字符串 </param>
        
///   <returns></returns>
         public   static   string  strEncode( string  str)
        {
            str 
=  str.Replace( " & " , " &amp; " );
            str 
=  str.Replace( " ' " , " '' " );
            str 
=  str.Replace( " " " , " &quot; " );
            str 
=  str.Replace( "   " , " &nbsp; " );
            str 
=  str.Replace( " < " , " &lt; " );
            str 
=  str.Replace( " > " , " &gt; " );
            str 
=  str.Replace( " " , " <br> " );
            
return  str;
        }


        
///   <summary>
        
///  对 HTML 字符串进行解码操作
        
///   </summary>
        
///   <param name="str"> 字符串 </param>
        
///   <returns></returns>
         public   static   string  strDecode( string  str)
        {
            str 
=  str.Replace( " <br> " , " " );
            str 
=  str.Replace( " &gt; " , " > " );
            str 
=  str.Replace( " &lt; " , " < " );
            str 
=  str.Replace( " &nbsp; " , "   " );
            str 
=  str.Replace( " &quot; " , " " " );
            
return  str;
        }

        
///   <summary>
        
///  对脚本程序进行处理
        
///   </summary>
        
///   <param name="str"></param>
        
///   <returns></returns>
         public   static   string  convertScript( string  str)
        {
            
string  strResult  =   "" ;
            
if (str  !=   "" )
            {
                StringReader sr 
=   new  StringReader(str);
                
string  rl;
                
do
                {
                    strResult 
+=  sr.ReadLine();
                } 
while ((rl  =  sr.ReadLine())  !=   null );
            }

            strResult 
=  strResult.Replace( " " " , " &quot; " );

            
return  strResult;
        }


        
///   <summary>
        
///  将一个字符串以某一特定字符分割成字符串数组
        
///   </summary>
        
///   <param name="Strings"> 字符串 </param>
        
///   <param name="str"> 分割字符 </param>
        
///   <returns> string[] </returns>
         public   static   string [] SplitArray( string  Strings,  char  str)
        {
            
string [] strArray  =  Strings.Trim().Split( new   char []{str});

            
return  strArray;
        }

/*
        /// <summary>
        /// 将一个字符串以某一字符分割成数组
        /// </summary>
        /// <param name="Strings">字符串</param>
        /// <param name="str">分割字符</param>
        /// <returns>string[]</returns>
        public static string[] SplitArray(string Strings, string str)
        {
            Regex r = new Regex(str);
            string[] strArray = r.Split(Strings.Trim());

            return strArray;
        }

*/

        
///   <summary>
        
///  检测一个字符串,是否存在于一个以固定分割符分割的字符串中
        
///   </summary>
        
///   <param name="str"> 字符串 </param>
        
///   <param name="Strings"> 固定分割符分割的字符串 </param>
        
///   <param name="Str"> 分割符 </param>
        
///   <returns></returns>
         public   static   bool  InArray( string  str,  string  Strings,  char  Str)
        {
            
bool  blResult  =   false ;

            
string [] array  =  SplitArray(Strings, Str);
            
for ( int  i  =   0 ; i  <  array.Length; i ++ )
            {
                
if (str  ==  array[i])
                {
                    blResult 
=   true ;
                    
break ;
                }
            }

            
return  blResult;
        }

/*
        /// <summary>
        /// 检测一个字符串,是否存在于一个以固定分割符分割的字符串中
        /// </summary>
        /// <param name="str">字符串</param>
        /// <param name="Strings">固定分割符分割的字符串</param>
        /// <param name="Str">分割符</param>
        /// <returns></returns>
        public static bool InArray(string str, string Strings, string Str)
        {
            bool blResult = false;

            string[] array = SplitArray(Strings, Str);
            for(int i = 0; i < array.Length; i++)
            {
                if(str == array[i])
                {
                    blResult = true;
                    break;
                }
            }

            return blResult;
        }
        
*/

        
///   <summary>
        
///  检测一个字符串,是否存在于一个以固定分割符分割的字符串中
        
///   </summary>
        
///   <param name="str"> 字符串 </param>
        
///   <param name="array"> 字符串数组 </param>
        
///   <returns></returns>
         public   static   bool  InArray( string  str,  string [] array)
        {
            
bool  blResult  =   false ;

            
for ( int  i  =   0 ; i  <  array.Length; i ++ )
            {
                
if (str  ==  array[i])
                {
                    blResult 
=   true ;
                    
break ;
                }
            }

            
return  blResult;
        }


        
///   <summary>
        
///  检测值是否有效,为 null 或 "" 均为无效
        
///   </summary>
        
///   <param name="obj"> 要检测的值 </param>
        
///   <returns></returns>
         public   static   bool  CheckValiable( object  obj)
        {
            
if ( Object.Equals(obj,  null ||  Object.Equals(obj,  string .Empty) )
                
return   false ;
            
else
                
return   true ;
        }
        
#endregion
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值