实例介绍
【实例简介】
【实例截图】

【核心代码】
using System;
using System.Collections.Generic;
using System.Data;
using System.DirectoryServices;
using System.IO;
using System.Management;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Xml;
namespace CrazyIIS
{
class WebAllInfo
{
public Int32 Id { get; set; }
public string Web { get; set; }
public string AppPoolId { get; set; }
public string Path { get; set; }
public bool ServerAutoStart { get; set; }
public string ServerBindings { get; set; }
}
class Comm
{
public static bool IsReg()
{
return true;
try
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("CrazyIIS.xml");
return Regex.Replace(Encrypt(xmlDoc.SelectSingleNode("//Reg/PC").InnerText xmlDoc.SelectSingleNode("//Reg/UserName").InnerText, "柳永法的CrazyIIS"), "(.{4})", "-$1").Substring(1) == xmlDoc.SelectSingleNode("//Reg/SN").InnerText.Trim().Substring(10);
}
catch
{
return false;
}
}
public static string Encrypt(string strEncrypt, string strKey)
{
try
{
byte[] keyArray = UTF8Encoding.UTF8.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(strKey, "md5"));
byte[] strEncryptArray = UTF8Encoding.UTF8.GetBytes(strEncrypt);
byte[] resultArray = null;
using (RijndaelManaged rDel = new RijndaelManaged())
{
rDel.Key = keyArray;
rDel.Mode = CipherMode.ECB;
rDel.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = rDel.CreateEncryptor();
resultArray = cTransform.TransformFinalBlock(strEncryptArray, 0, strEncryptArray.Length);
}
//return Convert.ToBase64String(resultArray, 0, resultArray.Length);
return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(Convert.ToBase64String(resultArray, 0, resultArray.Length), "MD5");
}
catch
{
return null;
}
}
public static bool RemoteFileExists(string furl)
{
try
{
HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(furl);
HttpWebResponse myRes = (HttpWebResponse)myReq.GetResponse();
if (myRes.ContentLength > 0)
{
myRes.Close();
return true;
}
else
{
myRes.Close();
return false;
}
}
catch
{
return false;
}
}
#region MetaBase
public static string MetaBasePath()
{
return Environment.SystemDirectory @"\inetsrv\MetaBase.xml";
}
public static string MetaBaseSave()
{
try
{
DirectoryEntry dir = new DirectoryEntry("IIS://localhost");
dir.Invoke("SaveData", new object[0]);//保存配置到MetaBase.xml
return "OK";
}
catch (Exception ex)
{
return ex.Message;
}
}
public static void MetaBaseBackup(string name)
{
Comm.MetaBaseSave();
DirectoryEntry dir = new DirectoryEntry("IIS://localhost");
dir.Invoke("Backup", new object[3] { name, 1, 1 });
}
public static string MetaBaseRestore(string name)
{
try
{
DirectoryEntry dir = new DirectoryEntry("IIS://localhost");
dir.Invoke("Restore", new object[3] { name, 1, 0 });
return "OK";
}
catch (Exception ex)
{
return ex.Message;
}
}
public static void MetaBaseRestore2Install()
{
MetaBaseRestore(MetaBaseFirstFileInfo().Name.Split('.')[0]);
}
public static FileInfo MetaBaseFirstFileInfo()
{
FileInfo FirstFile = null;
foreach (FileInfo item in new DirectoryInfo(Environment.SystemDirectory @"\inetsrv\MetaBack\").GetFiles("*.MD1"))
{
if (FirstFile == null)
{
FirstFile = item;
}
else
{
if (item.LastWriteTime < FirstFile.LastWriteTime)
{
FirstFile = item;
}
}
}
return FirstFile;
}
#endregion
public static DataTable GetAllWebInfo()
{
DataTable WebInfo = new DataTable();
WebInfo.Columns.Add("Id");
WebInfo.Columns.Add("Web");
WebInfo.Columns.Add("AppPoolId");
WebInfo.Columns.Add("Path");
WebInfo.Columns.Add("ServerAutoStart");
WebInfo.Columns.Add("ServerBindings");
WebInfo.Columns["Id"].DataType = typeof(Int32);
WebInfo.Columns["ServerAutoStart"].DataType = typeof(Boolean);
Dictionary<string, WebAllInfo> dict = GetAllWebInfoDict();
foreach (var item in dict.Values)
{
WebInfo.Rows.Add(item.Id, item.Web, item.AppPoolId, item.Path, item.ServerAutoStart, item.ServerBindings);
}
return WebInfo;
}
public static Dictionary<string, WebAllInfo> GetAllWebInfoDict()
{
MetaBaseSave();
XmlDocument doc = new XmlDocument();
doc.Load(Comm.MetaBasePath());
XmlNamespaceManager xnm = new XmlNamespaceManager(doc.NameTable);
xnm.AddNamespace("mxh", "urn:microsoft-catalog:XML_Metabase_V64_0");
string Default_AppPoolId = doc.SelectSingleNode("/mxh:configuration/mxh:MBProperty/mxh:IIsWebService", xnm).Attributes["AppPoolId"].Value;
XmlNodeList IIsWebServer = doc.SelectNodes("/mxh:configuration/mxh:MBProperty/mxh:IIsWebServer", xnm);
XmlNodeList IIsWebVirtualDir = doc.SelectNodes("/mxh:configuration/mxh:MBProperty/mxh:IIsWebVirtualDir", xnm);
Dictionary<string, WebAllInfo> _WebAllInfo = new Dictionary<string, WebAllInfo>();
foreach (XmlNode item in IIsWebServer)
{
Match m = Regex.Match(item.Attributes["Location"].Value, "/LM/W3SVC/(\\d )", RegexOptions.IgnoreCase);
if (!m.Success)
{
continue;
}
string Id = m.Groups[1].Value;
WebAllInfo info = new WebAllInfo();
info.Id = Convert.ToInt32(Id);
info.Web = item.Attributes["ServerComment"].Value;
info.ServerAutoStart = item.Attributes["ServerAutoStart"] == null ? true : Convert.ToBoolean(item.Attributes["ServerAutoStart"].Value);
info.ServerBindings = item.Attributes["ServerBindings"].Value.Replace("\r\n", ",");
_WebAllInfo.Add(Id, info);
}
foreach (XmlNode item in IIsWebVirtualDir)
{
Match m = Regex.Match(item.Attributes["Location"].Value, "/LM/W3SVC/(\\d )/root", RegexOptions.IgnoreCase);
if (!m.Success)
{
continue;
}
string Id = m.Groups[1].Value;
_WebAllInfo[Id].AppPoolId = item.Attributes["AppPoolId"] == null ? Default_AppPoolId : item.Attributes["AppPoolId"].Value;
_WebAllInfo[Id].Path = item.Attributes["Path"].Value;
}
return _WebAllInfo;
}
//public static DataTable GetAllWebInfo2()
//{
// //速度慢
// DataTable WebInfo = new DataTable();
// WebInfo.Columns.Add("Id");
// WebInfo.Columns.Add("Web");
// WebInfo.Columns.Add("AppPoolId");
// WebInfo.Columns.Add("Path");
// WebInfo.Columns.Add("ServerAutoStart");
// WebInfo.Columns.Add("ServerBindings");
// WebInfo.Columns["Id"].DataType = typeof(Int32);
// WebInfo.Columns["ServerAutoStart"].DataType = typeof(Boolean);
// IISConfig.Metabase metabase = new IISConfig.Metabase();
// metabase.OpenLocalMachine();
// IISConfig.IKey key = metabase.GetKeyFromPath("/LM/W3SVC");
// foreach (var item in key.Subkeys)
// {
// if (item.GetRecord(1002).Data.ToString() == "IIsWebServer")
// {
// WebInfo.Rows.Add(
// item.Name,
// item.GetRecord(1015).Data,
// item.Subkeys[0].GetRecord(9101).Data,
// item.Subkeys[0].GetRecord(3001).Data,
// item.GetRecord(1017) == null ? true : false,
// string.Join(",", item.GetRecord(1023).Data as string[])
// );
// }
// }
// return WebInfo;
//}
#region NewWebSiteId
public static string GetNewWebSiteID(List<Int32> List)
{
List.Sort();
int i = 1;
foreach (int j in List)
{
if (i == j)
{
i ;
}
else
{
break;
}
}
return i.ToString();
}
public static List<Int32> GetAllWebSiteID()
{
List<Int32> list = new List<Int32>();
//DirectoryEntry dir = new DirectoryEntry("IIS://localhost/w3svc");
//foreach (DirectoryEntry child in dir.Children)
//{
// if (child.SchemaClassName == "IIsWebServer")
// {
// list.Add(Convert.ToInt32(child.Name));
// }
//}
MetaBaseSave();
string MetaBase = File.ReadAllText(Comm.MetaBasePath());
foreach (Match item in Regex.Matches(MetaBase, "W3SVC/(\\d )\""))
{
list.Add(Convert.ToInt32(item.Groups[1].Value));
}
list.Sort();
return list;
}
#endregion
#region AppPoolId
public static List<string> GetAppPoolsUsed()
{
Comm.MetaBaseSave();
List<string> list = new List<string>();
string MetaBase = File.ReadAllText(Comm.MetaBasePath());
foreach (Match item in Regex.Matches(MetaBase, @"AppPoolId=""(. ?)"""))
{
if (!list.Contains(item.Groups[1].Value))
{
list.Add(item.Groups[1].Value);
}
}
return list;
}
public static List<string> GetAppPools()
{
List<string> list = new List<string>();
IISConfig.Metabase metabase = new IISConfig.Metabase();
metabase.OpenLocalMachine();
IISConfig.IKey key = metabase.GetKeyFromPath("/LM/W3SVC/AppPools");
foreach (var item in key.Subkeys)
{
list.Add(item.Name);
}
metabase.Close();
return list;
}
//public static List<string> GetAppPools2()
//{
// DirectoryEntry dir = new DirectoryEntry("IIS://localhost/w3svc/AppPools");
// List<string> list = new List<string>();
// foreach (DirectoryEntry child in dir.Children)
// {
// list.Add(child.Name);
// }
// return list;
//}
//public static List<string> GetAppPools()
//{
// Comm.MetaBaseSave();
// List<string> list = new List<string>();
// string MetaBase = File.ReadAllText(Comm.MetaBasePath());
// foreach (Match item in Regex.Matches(MetaBase, @"/LM/W3SVC/AppPools/(. ?)"""))
// {
// list.Add(item.Groups[1].Value);
// }
// return list;
//}
#endregion
#region Attributes
public static string GetPropertyValue(DirectoryEntry dir, string Properties)
{
string split = "柳永法";
PropertyValueCollection s = dir.Properties[Properties];
if (s.Count == 0)
{
return s[0].ToString();
}
else
{
string temp = "";
for (int i = 0; i < s.Count; i )
{
temp = split s[i].ToString();
}
return temp.Substring(split.Length);
}
}
public static string[] GetPropertyValueCollection(string str)
{
string split = "柳永法";
return Regex.Split(str, split);
}
public static string GetDefaultAnonymousUserName()
{
return GetAttributesByPath("/mxh:configuration/mxh:MBProperty/mxh:IIsWebService", "AnonymousUserName");
}
public static string GetDefaultAppPoolId()
{
return GetIIsWebServiceAttributesByPath("AppPoolId");
}
public static string GetIIsWebServiceAttributesByPath(string Attributes)
{
return GetAttributesByPath("/mxh:configuration/mxh:MBProperty/mxh:IIsWebService", Attributes);
}
public static string GetAttributesByPath(string Path, string Attributes)
{
XmlDocument doc = new XmlDocument();
doc.Load(Comm.MetaBasePath());
XmlNamespaceManager xnm = new XmlNamespaceManager(doc.NameTable);
xnm.AddNamespace("mxh", "urn:microsoft-catalog:XML_Metabase_V64_0");
if (doc.SelectSingleNode(Path, xnm).Attributes[Attributes] == null)
{
return null;
}
else
{
return doc.SelectSingleNode(Path, xnm).Attributes[Attributes].Value;
}
}
#endregion
public static string[] GetScriptMaps(string ver)
{
//如果他有一些其它脚本映射,如:php,jsp,就会出问题
string s = "";
switch (ver.ToLower())
{
case "v1.1.4322":
s = @".asp,C:\WINDOWS\system32\inetsrv\asp.dll,5,GET,HEAD,POST,TRACE
.cer,C:\WINDOWS\system32\inetsrv\asp.dll,5,GET,HEAD,POST,TRACE
.cdx,C:\WINDOWS\system32\inetsrv\asp.dll,5,GET,HEAD,POST,TRACE
.asa,C:\WINDOWS\system32\inetsrv\asp.dll,5,GET,HEAD,POST,TRACE
.idc,C:\WINDOWS\system32\inetsrv\httpodbc.dll,5,GET,POST
.shtm,C:\WINDOWS\system32\inetsrv\ssinc.dll,5,GET,POST
.shtml,C:\WINDOWS\system32\inetsrv\ssinc.dll,5,GET,POST
.stm,C:\WINDOWS\system32\inetsrv\ssinc.dll,5,GET,POST
.asax,C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.ascx,C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.ashx,C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG
.asmx,C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG
.aspx,C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG
.axd,C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG
.vsdisco,C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG
.rem,C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG
.soap,C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG
.config,C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.cs,C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.csproj,C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.vb,C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.vbproj,C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.webinfo,C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.licx,C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.resx,C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.resources,C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG";
break;
case "v2.0.50727":
s = @".asp,c:\WINDOWS\system32\inetsrv\asp.dll,5,GET,HEAD,POST,TRACE
.cer,C:\WINDOWS\system32\inetsrv\asp.dll,5,GET,HEAD,POST,TRACE
.cdx,C:\WINDOWS\system32\inetsrv\asp.dll,5,GET,HEAD,POST,TRACE
.asa,C:\WINDOWS\system32\inetsrv\asp.dll,5,GET,HEAD,POST,TRACE
.idc,C:\WINDOWS\system32\inetsrv\httpodbc.dll,5,GET,POST
.shtm,C:\WINDOWS\system32\inetsrv\ssinc.dll,5,GET,POST
.shtml,C:\WINDOWS\system32\inetsrv\ssinc.dll,5,GET,POST
.stm,C:\WINDOWS\system32\inetsrv\ssinc.dll,5,GET,POST
.asax,C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.ascx,C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.ashx,C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG
.asmx,C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG
.aspx,C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG
.axd,C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG
.vsdisco,C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG
.rem,C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG
.soap,C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG
.config,C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.cs,C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.csproj,C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.vb,C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.vbproj,C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.webinfo,C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.licx,C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.resx,C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.resources,C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.master,C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.skin,C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.compiled,C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.browser,C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.mdb,C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.jsl,C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.vjsproj,C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.sitemap,C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.msgx,C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG
.ad,C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.dd,C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.ldd,C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.sd,C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.cd,C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.adprototype,C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.lddprototype,C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.sdm,C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.sdmDocument,C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.ldb,C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.svc,C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG
.mdf,C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.ldf,C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.java,C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.exclude,C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.refresh,C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG";
break;
default:
break;
}
s = Regex.Replace(s, @"C:\\WINDOWS", Environment.GetEnvironmentVariable("windir").ToUpper(), RegexOptions.IgnoreCase);
return Regex.Split(s, "\\s ");
}
/// <summary>
/// 取得网页源码
/// </summary>
/// <param name="url">网页地址,eg: "http://www.yongfa365.com/" </param>
/// <param name="charset">网页编码,eg: Encoding.UTF8</param>
/// <returns>返回网页源文件</returns>
public static string GetHtmlSource(string url, Encoding charset)
{
//处理内容
string html = "";
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "Get";
request.ContentType = "application/x-www-form-urlencoded";
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2;)";
request.Accept = "text/html";
request.Referer = url;
request.KeepAlive = true;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream stream = response.GetResponseStream();
StreamReader reader = new StreamReader(stream, charset);
html = reader.ReadToEnd();
stream.Close();
}
catch (Exception e)
{
Console.Write(e.Message);
}
return html;
}
public static void Write2hosts(string[] Hosts, bool Delete)
{
string hostsPath = Environment.SystemDirectory @"\drivers\etc\hosts";
string hostsNew = "";
string hostsCnt = "";
if (Delete)
{
hostsCnt = "127.0.0.1 localhost";
}
else
{
hostsCnt = File.ReadAllText(hostsPath);
}
for (int i = 0; i < Hosts.Length; i = i 2)
{
string IP = Hosts[i];
string Domain = Hosts[i 1];
if (!Regex.IsMatch(hostsCnt, IP "\\s " Domain))
{
hostsNew = "\r\n" IP " " Domain;
}
}
if (!File.Exists(hostsPath))
{
File.WriteAllText(hostsPath, "");
}
FileInfo f = new FileInfo(hostsPath);
f.IsReadOnly = false;
File.WriteAllText(hostsPath, hostsCnt hostsNew);
f.IsReadOnly = true;
}
public static void DataGridViewSet(DataGridView dgv)
{
dgv.AllowUserToAddRows = false;
dgv.AllowUserToDeleteRows = false;
dgv.AllowUserToOrderColumns = true;
dgv.AllowUserToResizeRows = false;
dgv.RowHeadersWidth = 20;
dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
dgv.ReadOnly = true;
}
//取CPU编号
public static string GetCpuID()
{
try
{
ManagementClass mc = new ManagementClass("Win32_Processor");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
return mo.Properties["ProcessorId"].Value.ToString();
}
return "";
}
catch
{
return "";
}
}
public static string Random(int length)
{
return Random((int)DateTime.Now.Ticks, length);
}
public static string Random(int zj, int length)
{
string Vchar = @"~!@#$%^&*()_ {}:""<>?/.,';][=-\|0123456789ABCDEFGHIJKLMNPQRSTUVWXYZabcdefghijklmnpqrstuvwxyz";
Random rand = new Random(zj);
string result = "";
for (int i = 0; i < length; i )
{
int t = rand.Next(Vchar.Length);
result = Vchar[t];
}
return result;
}
public static string RandomNumber(int zj, int length)
{
string Vchar = "0123456789";
Random rand = new Random(zj);
string result = "";
for (int i = 0; i < length; i )
{
int t = rand.Next(Vchar.Length);
result = Vchar[t];
}
return result;
}
public static string Random2(int length)
{
string Vchar = @"0123456789ABCDEFGHIJKLMNPQRSTUVWXYZ";
Random rand = new Random();
string result = "";
for (int i = 0; i < length; i )
{
int t = rand.Next(Vchar.Length);
result = Vchar[t];
}
return result;
}
}
}
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论