.NET开发中你可能会用到的常用方法总结

承接 .NET开发中你可能会用到的常用方法总结。
放在一篇中整理起来比较难受,继续在这里进行添加。
ASP.NET获取服务器信息方法

if  ( ! IsPostBack)
            {
                Label1.Text 
=   " 服务器名称: " + Server.MachineName; // 服务器名称
                Label2.Text  =   " 服务器IP地址: "   +  Request.ServerVariables[ " LOCAL_ADDR " ]; // 服务器IP地址
                Label3.Text  =   " 服务器域名: "   +  Request.ServerVariables[ " SERVER_NAME " ]; // 服务器域名
                Label4.Text  =   " .NET解释引擎版本: "   +   " .NET CLR "   +  Environment.Version.Major  +   " . "   +  Environment.Version.Minor  +   " . "   +  Environment.Version.Build  +   " . "   +  Environment.Version.Revision; // .NET解释引擎版本
                Label5.Text  =   " 服务器操作系统版本: "   +  Environment.OSVersion.ToString(); // 服务器操作系统版本
                Label6.Text  =   " 服务器IIS版本: "   +  Request.ServerVariables[ " SERVER_SOFTWARE " ]; // 服务器IIS版本
                Label7.Text  =   " HTTP访问端口: "   +  Request.ServerVariables[ " SERVER_PORT " ]; // HTTP访问端口
                Label8.Text  =   " 虚拟目录的绝对路径: "   +  Request.ServerVariables[ " APPL_RHYSICAL_PATH " ]; // 虚拟目录的绝对路径
                Label9.Text  =   " 执行文件的绝对路径: "   +  Request.ServerVariables[ " PATH_TRANSLATED " ]; // 执行文件的绝对路径
                Label10.Text  =   " 虚拟目录Session总数: "   +  Session.Contents.Count.ToString(); // 虚拟目录Session总数
                Label11.Text  =   " 虚拟目录Application总数: "   +  Application.Contents.Count.ToString(); // 虚拟目录Application总数
                Label12.Text  =   " 域名主机: "   +  Request.ServerVariables[ " HTTP_HOST " ]; // 域名主机
                Label13.Text  =   " 服务器区域语言: "   +  Request.ServerVariables[ " HTTP_ACCEPT_LANGUAGE " ]; // 服务器区域语言
                Label14.Text  =   " 用户信息: "   +  Request.ServerVariables[ " HTTP_USER_AGENT " ];
                Label14.Text
= " CPU个数: " + Environment.GetEnvironmentVariable( " NUMBER_OF_PROCESSORS " ); // CPU个数
                Label15.Text  =   " CPU类型: "   +  Environment.GetEnvironmentVariable( " PROCESSOR_IDENTIFIER " ); // CPU类型
                Label16.Text  =   " 进程开始时间: "   +  GetPrStart(); // 进程开始时间
                Label17.Text  =   " AspNet 内存占用: "   +  GetAspNetN(); // AspNet 内存占用
                Label18.Text  =   " AspNet CPU时间: "   +  GetAspNetCpu(); // AspNet CPU时间
                Label19.Text  =   " FSO 文本文件读写: "   +  Check( " Scripting.FileSystemObject " ); // FSO 文本文件读写
                Label20.Text  =   " 应用程序占用内存 "   +  GetServerAppN(); // 应用程序占用内存
             }

ASP.NET获取客户端信息


客户端IP:Page.Request.UserHostAddress
用户信息:Page.User;
服务器电脑名称:Page.Server.MachineName
当前用户电脑名称: System.Net.Dns.GetHostName()
当前电脑名: System.Environment.MachineName
当前电脑所属网域: System.Environment.UserDomainName
当前电脑用户: System.Environment.UserName

浏览器类型:Request.Browser.Browser
浏览器标识:Request.Browser.Id
浏览器版本号:Request.Browser.Version
浏览器是不是测试版本:Request.Browser.Beta
浏览器的分辨率(像素):Request[
" width " ].ToString()  +   " * "   +  Request[ " height " ].ToString(); // 1280/1024

客户端的操作系统:Request.Browser.Platform
是不是win16系统:Request.Browser.Win16
是不是win32系统:Request.Browser.Win32


// 透过代理取IP
string  GetIp()
{
// 可以透过代理服务器
string  userIP  =  Request.ServerVariables[ " HTTP_X_FORWARDED_FOR " ];
if  (userIP  ==   null   ||  userIP  ==   "" )
{
// 没有代理服务器,如果有代理服务器获取的是代理服务器的IP
userIP  =  Request.ServerVariables[ " REMOTE_ADDR " ];
}
return  userIP;
}

 C#实现页面加载


protected   void  Page_Load( object  sender, EventArgs e)
    {
        Loading();
    }


public   void  Loading()
    {
        HttpContext hc 
=  HttpContext.Current;
        
// 创建一个页面居中的div
        hc.Response.Write( " <div id='loading'style='position: absolute; height: 100px; text-align: center;z-index: 9999; left: 50%; top: 50%; margin-top: -50px; margin-left: -175px;'>  " );
        hc.Response.Write(
" <br />页面正在加载中,请稍候<br /><br />  " );
        hc.Response.Write(
" <table border='0' cellpadding='0' cellspacing='0' style='background-image: url(images/Progress/plan-bg.gif);text-align: center; width: 300px;'>  " );
        hc.Response.Write(
" <tr><td style='height: 20px; text-align: center'><marquee direction='right' scrollamount='30' width='290px'> <img height='10' src='images/Progress/plan-wait.gif' width='270' /> " );
        hc.Response.Write(
" </marquee></td></tr></table></div> " );
        
// hc.Response.Write("<script>mydiv.innerText = '';</script>");
        hc.Response.Write( " <script type=text/javascript> " );
        
// 最重要是这句了,重写文档的onreadystatechange事件,判断文档是否加载完毕
        hc.Response.Write( " function document.onreadystatechange() " );
        hc.Response.Write(
@" { try  
                                   {
                                    if (document.readyState == 'complete') 
                                    {
                                         delNode('loading');
                                        
                                    }
                                   }
                                 catch(e)
                                    {
                                        alert('页面加载失败');
                                    }
                                                        } 

                            function delNode(nodeId)
                            {   
                                try
                                {   
                                      var div =document.getElementById(nodeId); 
                                      if(div !==null)
                                      {
                                          div.parentNode.removeChild(div);   
                                          div=null;    
                                          CollectGarbage(); 
                                      } 
                                }
                                catch(e)
                                {   
                                   alert('删除ID为'+nodeId+'的节点出现异常');
                                }   
                            }

                            
" );

        hc.Response.Write(
" </script> " );
        hc.Response.Flush();
    }

 Http请求图片显示:


public  Image byteArrayToImage()    
{        
WebRequest myWebRequest 
=  WebRequest.Create( " 图片地址 " );        
using  (WebResponse myWebResponse  =  myWebRequest.GetResponse())        
{            
using  (Stream ReceiveStream  =  myWebResponse.GetResponseStream())            {                 byte [] byteArrayIn  =   new   byte [ReceiveStream.Length];                
using  (MemoryStream ms  =   new  MemoryStream(byteArrayIn))                
{       
Image.FromStream(ms);                
}            
}        
}
}

你可能感兴趣的:(.NET开发中你可能会用到的常用方法总结)