利用Windows內建的taskkill及tskill指令關閉程式,只需將軟體名稱帶入function即可。
例如
Taskkill("notepad.exe");
或
Taskkill("notepad*");
/// <summary> /// 關閉軟體 /// </summary> /// <param name="ProcessName"></param> private void Taskkill(string ProcessName) { try { using (Process P = new Process()) { P.StartInfo = new ProcessStartInfo() { FileName = "taskkill", CreateNoWindow = true, WindowStyle = ProcessWindowStyle.Hidden, Arguments = "/F /IM \"" + ProcessName + "\"" }; P.Start(); P.WaitForExit(60000); } } catch { using (Process P = new Process()) { P.StartInfo = new ProcessStartInfo() { FileName = "tskill", CreateNoWindow = true, WindowStyle = ProcessWindowStyle.Hidden, Arguments = "\"" + ProcessName + "\" /A /V" }; P.Start(); P.WaitForExit(60000); } } }
WebClient 內建並沒有 Timeout 指令,透過以下方法加入:
using System; using System.Net; namespace XXX { public class MyWebClient : WebClient { protected override WebRequest GetWebRequest(Uri uri) { WebRequest WR = base.GetWebRequest(uri); WR.Timeout = 30 * 1000; return WR; } } }
Timeout 時間為毫秒,30 * 1000 表示 30秒 為超時上限時間。
使用方法如下:
MyWebClient MWC = new MyWebClient(); string HTML = MWC.DownloadString("http://www.google.com.tw/"); Console.WriteLine(HTML);
如果要監測某個 Port 是否正常,可以使用以下方式進行偵測。
static bool Check(string IPStr, int Port, int Timeout) { bool success = false; try { using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) success = socket.BeginConnect(IPAddress.Parse(IPStr), Port, null, null).AsyncWaitHandle.WaitOne(Timeout, true); } catch { } return success; }
利用以下四種方法可以取得電腦名稱:
//Windows 及 LINUX 都正常 Environment.MachineName System.Net.Dns.GetHostName() System.Windows.Forms.SystemInformation.ComputerName //Windows 正常 System.Environment.GetEnvironmentVariable("COMPUTERNAME")
本篇測試 JSON TO String 及 String TO JSON,使用以下三種方式 JavaScriptSerializer、Json.NET、fastJSON 進行效能測試 。
測試原始碼下載:fastJsonDemo
原始碼如下:
using System; using System.Diagnostics; using System.Web.Script.Serialization; using fastJSON; using Newtonsoft.Json; namespace fastJsonDemo { public partial class Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { testSpeed(); Write("<BR>"); testConvertDatetime(); } protected void testSpeed() { int i_testCount = 30000; User[] l_users = new User[i_testCount]; for (int i = 0; i < i_testCount; i++) l_users[i] = new User { ID = i, Name = "TEST", Age = 20 + i, Address = "100 臺北市..." + i.ToString() + "號" }; /* 使用 JavaScriptSerializer */ testJavaScriptSerializer(l_users); /* 使用 Json.NET */ testJsonDotNET(l_users); /* 使用 fastJSON */ testfastJSON(l_users); } protected void testJavaScriptSerializer(User[] l_users) { string s_jsonstr = string.Empty; Stopwatch sw = new Stopwatch(); Write("<BR>使用 JavaScriptSerializer 物件 to JSON<BR>", "#524CE4", true); sw.Reset(); sw.Start(); s_jsonstr = ConvertObject2JSON(l_users); sw.Stop(); Write(sw.Elapsed.Milliseconds.ToString() + " ms<BR>", "#666", true); Write("使用 JavaScriptSerializer JSON to 物件<BR>", "#524CE4", true); sw.Reset(); sw.Start(); User[] l_tempusers = (User[])ConvertJSON2Object(s_jsonstr, typeof(User[])); sw.Stop(); Write(sw.Elapsed.Milliseconds.ToString() + " ms<BR>", "#666", true); Write(ConvertObject2JSON(new User[] { l_users[0], l_users[1], l_users[2] }) + "<BR>", "#F74B14", false); } protected void testJsonDotNET(User[] l_users) { string jsonstr = string.Empty; Stopwatch sw = new Stopwatch(); Write("<BR>使用 Json.NET 物件 to JSON<BR>", "#524CE4", true); sw.Reset(); sw.Start(); jsonstr = JsonConvert.SerializeObject(l_users); sw.Stop(); Write(sw.Elapsed.Milliseconds.ToString() + " ms<BR>", "#666", true); Write("使用 Json.NET JSON to 物件<BR>", "#524CE4", true); sw.Reset(); sw.Start(); User[] l_tempusers = JsonConvert.DeserializeObject<User[]>(jsonstr); sw.Stop(); Write(sw.Elapsed.Milliseconds.ToString() + " ms<BR>", "#666", true); Write(JsonConvert.SerializeObject(new User[] { l_users[0], l_users[1], l_users[2] }) + "<BR>", "#F74B14", false); } protected void testfastJSON(User[] l_users) { string s_jsonstr = string.Empty; Stopwatch sw = new Stopwatch(); Write("<BR>使用 fastJSON 物件 to JSON<BR>", "#524CE4", true); sw.Reset(); sw.Start(); s_jsonstr = JSON.Instance.ToJSON(l_users); sw.Stop(); Write(sw.Elapsed.Milliseconds.ToString() + " ms<BR>", "#666", true); Write("使用 fastJSON JSON to 物件<BR>", "#524CE4", true); sw.Reset(); sw.Start(); User[] l_tempusers = fastJSON.JSON.Instance.ToObject(s_jsonstr) as User[]; sw.Stop(); Write(sw.Elapsed.Milliseconds.ToString() + " ms<BR>", "#666", true); Write(JSON.Instance.ToJSON(new User[] { l_users[0], l_users[1], l_users[2] }) + "<BR>", "#F74B14", false); } protected void testConvertDatetime() { string s_js = string.Empty; DateTime dt = DateTime.Now; Write("<BR>使用 JavaScriptSerializer <BR>", "#8F3535", true); s_js = ConvertObject2JSON(dt); Write(s_js + " <BR>", "#666", true); Write(((DateTime)ConvertJSON2Object(s_js, typeof(DateTime))).ToString() + " <BR>", "#666", true); Write("<BR>使用 Json.NET <BR>", "#8F3535", true); s_js = JsonConvert.SerializeObject(dt); Write(s_js +"<BR>", "#666", true); Write(JsonConvert.DeserializeObject<DateTime>(s_js).ToString() + "<BR>", "#666", true); Write("<BR>使用 fastJSON <BR>", "#8F3535", true); s_js = JSON.Instance.ToJSON(dt); Write(s_js + " <BR>", "#666", true); Write(DateTime.Parse(JSON.Instance.ToObject(s_js).ToString()).ToString() + " <BR>", "#666", true); } protected string ConvertObject2JSON(object Source) { JavaScriptSerializer JSONSerializer = new JavaScriptSerializer(); JSONSerializer.RecursionLimit = Int32.MaxValue; return JSONSerializer.Serialize(Source); } protected object ConvertJSON2Object(string JSON, Type ObjectType) { JavaScriptSerializer JSONSerializer = new JavaScriptSerializer(); JSONSerializer.RecursionLimit = Int32.MaxValue; return JSONSerializer.Deserialize(JSON, ObjectType); } protected void Write(string word, string color, bool bord) { if (bord) Response.Write("<b><font color=\"" + color + "\">" + word + "</font></b>"); else Response.Write("<font color=\"" + color + "\">" + word + "</font>"); } protected void Write(string word) { Response.Write(word); } } public class User { public long ID { get; set; } public string Name { get; set; } public int Age { get; set; } public string Address { get; set; } } }
如果您參考我的上一篇:
無法正確取得 CPU 溫度,其實算是正常 =.= 因為我主機板也取不到!
上面這種方法,有些主機板會取得不到資料,上網找了很久,找到另外一種方法。
利用 Open Hardware Monitor 這套軟體,這套軟體可以取得 MB、CPU、RAM 溫度 及 風扇轉速,所以只需要參考 OpenHardwareMonitor.dll 就可以取得您要的資料了!
首先到 Open Hardware Monitor 下載軟體
官方網站:http://openhardwaremonitor.org/
本地下載:openhardwaremonitor-v0.5.1-beta
然後將檔案解壓縮,會發現有一個 OpenHardwareMonitorLib.dll 檔案,將您的 .Net 專案 參考 OpenHardwareMonitorLib.dll。
到官方下載:http://www.amcharts.com/download/
本站下載:amcharts_2.10.3 、amstockchart_2.10.3
利用 ManagementObjectSearcher 取得所有 CPU 溫度,如果有多顆 CPU 就會有多個數值,以下是把所有數值平均。
using System.Management; Double CPUTPRT = 0, CPUNUM = 0; ManagementObjectSearcher mos = new ManagementObjectSearcher(@"root\WMI", "Select * From MSAcpi_ThermalZoneTemperature"); foreach (System.Management.ManagementObject mo in mos.Get()) { CPUNUM++; CPUTPRT += Convert.ToDouble(Convert.ToDouble(mo.GetPropertyValue("CurrentTemperature").ToString()) - 2732) / 10; } CPUTPRT /= CPUNUM; Console.WriteLine("CPU 溫度 : " + CPUTPRT.ToString() + " °C"); Console.Read();
如果您使用以上方法,無法正確 CPU 溫度,每次值都一樣,可以參考下面這篇方法:
C# 取得 CPU 平均 溫度 利用 OpenHardwareMonitor.dll
如果系統更新 .NET Framework 發生更新失敗時,可以使用此方式解決。
首先下載 .NET Framework 移除工具:
如果你遇到 Uint64 還不夠處理的窘境,可以試試看 BigInteger Class,就可以處理超大數值。
官方網站:http://www.codeproject.com/Articles/2728/C-BigInteger-Class
本地下載:BigInteger_src