習慣於用某一種語言做開發,時間長了以後很多人可能會對其它語言有排斥或抵觸,其實... 退一步海闊天空!
比如:經常看到有人問"如何用c# ping 網址?",園子裡搜尋了一下:[c#下實現ping操作]這篇算是總結得比較全面了,但其實...如果願意暫時從C#的世界中退出來環顧一下周圍,方法更簡單:
先添加對Microsoft.VisualBasic的引用,然後參考下面的代碼:
...using Microsoft.VisualBasic.Devices;...Computer myComputer = new Computer();Boolean ping = myComputer.Network.Ping("www.cnblogs.com");if (ping){Response.Write("您的機器可以上部落格園!");}else{Response.Write("部落格園網站無法訪問!");}
再比如:如何用c#做漢字的簡<-->繁轉換?
using Microsoft.VisualBasic;...Response.Write(Strings.StrConv("菩提樹下的楊過", VbStrConv.TraditionalChinese, 0));
這樣就將簡體中文換為繁體了,類似的:
Response.Write(Strings.StrConv("菩提樹下的楊過", VbStrConv.SimplifiedChinese, 0));
這樣就可以將繁體中文轉換為簡體
如何得到當前webApplication的程式集名稱以運行時目錄?
using System;using Microsoft.VisualBasic.ApplicationServices;namespace aspxwebForm{ public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { ApplicationBase app = new ApplicationBase(); Response.Write("<br/>程式集名稱:" + app.Info.AssemblyName); Response.Write("<br/>程式集運行時所在目錄:" + app.Info.DirectoryPath); } }}
如何搜尋某目錄及其子目錄中檔案內容包含'abc'的檔案?
using System;using Microsoft.VisualBasic.Devices;using Microsoft.VisualBasic.FileIO;namespace aspxwebForm{ public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Computer myComputer = new Computer(); var list = myComputer.FileSystem.FindInFiles("c:\\temp\\", "abc", true, SearchOption.SearchAllSubDirectories); foreach (var item in list) { Response.Write(item + "<br/>"); } } }}
上面的代碼將搜尋c:\temp以所有子目錄中包含"abc"的檔案(不區分大小寫)
文中列出的幾個小應用只能算是拋磚引玉,如果大家發現VB.Net中更好玩、更簡潔的應用,歡迎回貼.