ASP.NET 中如何获取服务器端环境变量

作者:vkvi 来源:ITPOW(原创) 日期:2007-8-8

不复杂,用示例说明常见应用。

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    void Page_Load(object sender, EventArgs e)
    {
        NameValueCollection collection = Request.ServerVariables;
       
        list.Text = "";
       
        list.Text = "总共有 " + collection.Count.ToString() + " 个环境变量。<br><br>";
        for (int i = 0; i < collection.Count; i++)
        {
            list.Text += "环境变量名称:" + collection.GetKey(i) + "<br>" +
                         "值:" + collection.Get(i) + "<br>" +
                         "---<br>";
        }
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>ASP.NET 中如何获取服务器端环境变量 - ITPOW</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Label ID="list" runat="server"></asp:Label>
    </div>
    </form>
</body>
</html>

重要部分已经在代码中标明。

NameValueCollection 的名称空间是:

System.Collections.Specialized

该名称空间默认是导入的。

相关文章