深度优先

这个家伙好懒,除了文章什么都没留下

0%

ASP.NET获取客户端信息,暂时就这几个,有待添加~~

  1. 在ASP.NET中专用属性:
    获取服务器电脑名:Page.Server.ManchineName
    获取用户信息:Page.User
    获取客户端电脑名:Page.Request.UserHostName
    获取客户端电脑IP:Page.Request.UserHostAddress
  2. 在网络编程中的通用方法:
    获取当前电脑名:static System.Net.Dns.GetHostName()
    根据电脑名取出全部IP地址:static System.Net.Dns.Resolve(电脑名).AddressList
    也可根据IP地址取出电脑名:static System.Net.Dns.Resolve(IP地址).HostName
  3. 系统环境类的通用属性:
    当前电脑名:static System.Environment.MachineName
    当前电脑所属网域:static System.Environment.UserDomainName
    当前电脑用户:static System.Environment.UserName
    客户端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;
    服务器端的信息:
    服务器计算机名:”http://“ + HttpContext.Current.Request.Url.Host + HttpContext.Current.Request.ApplicationPath;
    服务器IIS版本: Request.ServerVariables[“Server_SoftWare”].ToString();
    服务器域名:Request.ServerVariables[“SERVER_NAME”].ToString();
    服务器域名:Request.ServerVariables[“HTTP_HOST”]
    服务器端口:Request.ServerVariables[“Server_Port”].ToString();
    服务器IP地址:Request.ServerVariables[“LOCAl_ADDR”]
    服务器脚本超时时间:(Server.ScriptTimeout / 1000).ToString() + “秒”;
    服务器操作系统:Environment.OSVersion.ToString();
    本文件所在文件夹:Request.PhysicalApplicationPath;
    服务器IE版本:Registry.LocalMachine.OpenSubKey(@”SOFTWARE\Microsoft\Internet Explorer\Version Vector”).Ge

对课本上的代码进行了一点的优化 1.获取文件的名称和文件的后缀名

引用了System.IO,

用Path.GetFileNamehe()取得文件名和Path.GetExtension获取文件的后缀

2.对上传的文件进行了重命名

采用Guid全局唯一标识进行命名

Guid.NewGuid().ToString()

3.根据日期创建文件夹,先判断文件夹是否存在,不存在就创建一个

创建文件夹:Directory.CreateDirectory(dir)

string dir = “/Images/“ + DateTime.Now.Year + “-“ + DateTime.Now.Month + “-“ + DateTime.Now.Day + “/“;

判断文件夹是否存在

Directory.Exists(Server.MapPath(dir))

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
protected void Button1_Click(object sender, EventArgs e)
{
if (Fileupload1.HasFile)
{
//获取文件名
string filePath = Path.GetFileName(Fileupload1.FileName);

//获取文件后缀
string fileExt = Path.GetExtension(filePath);

//检查文件类型 是否为图片文件
if (!IsImg(fileExt))
return;

//创建文件夹,以年月日创建
string dir = "/Images/" + DateTime.Now.Year + "-" + DateTime.Now.Month + "-" + DateTime.Now.Day + "/";

//判断文件夹是否存在,不存在就创建
if(!Directory.Exists(Server.MapPath(dir)))
Directory.CreateDirectory(Server.MapPath(dir));

//给图片进行重命名,Guid唯一标识
string newFileName = Guid.NewGuid().ToString();

//文件的相对路径
string fileDir = dir + newFileName + fileExt;

//上传到服务器
Fileupload1.SaveAs(Server.MapPath(fileDir));

//显示图片
Image1.ImageUrl = fileDir;
}
else
{
Response.Write("<Script>alert('请选择上传图片!')</Script>");
}
}

// 判断文件后缀名
private bool IsImg(string fileExt)
{
string[] ext = new string[] { ".jpg", ".png", ".gif", ".bmp", ".jpeg" };
for (int i = 0; i < ext.Length; i++)
{
if (fileExt.Equals(ext[i]))
return true;
}
return false;
}