【.NET】显示网站所在文件夹
using System.Net; public void GetServerFile() { string string1, string2; string[] array1; string1 = Server.MapPath(""); array1 = string1.Split(new string[] { @"\" }, StringSplitOptions.RemoveEmptyEntries); if (!((IList)new string[] { "web", "webfiles", "wwwroot" }).Contains(array1[array1.Length - 3].ToLower())) { string1 = array1[array1.Length - 3] + @"\" + array1[array1.Length - 2]; } else { string1 = array1[array1.Length - 2]; } string2 = Dns.GetHostEntry(Request.Url.Host).AddressList[0].ToString();//Dns.GetHostEntry()查找主机IP或主机名 Response.Write("ip: " + string2 + "<br />文件夹: " + string1); Response.End(); }
结果显示:
ip:192.xxx.xxx.xxx
文件夹:wangzhan(在wwwroot之后的文件夹名)
目的:直接访问域名查找,方便网站管理时,前端、程序查看网站所在位置,不用每次都去IIS对文件夹。