向Visual Basic程序員介紹My命名空間(二)
2024-07-21 02:21:10
供稿:網(wǎng)友
 
my.computer
my的另一個功能塊是computer對象。my.computer對象可以讓你訪問系統(tǒng)底層和應(yīng)用程序所在硬件平臺的信息。示例應(yīng)用程序的第二個選項卡展示了你在my.computer對象中能找到的有趣屬性。
 
圖三
my.computer 選項卡
控制這個網(wǎng)格的代碼如下:
myappds.mycomputer.addmycomputerrow("clock", _
 my.computer.clock.localtime)
myappds.mycomputer.addmycomputerrow("gmt time", _
 my.computer.clock.gmttime)
myappds.mycomputer.addmycomputerrow("tick count", _
 my.computer.clock.tickcount)
myappds.mycomputer.addmycomputerrow("gmt time", _
 my.computer.eventlogs.item("application").readentries().item(1).message)
myappds.mycomputer.addmycomputerrow("crtl key down", _
 my.computer.keyboard.ctrlkeydown.tostring())
myappds.mycomputer.addmycomputerrow("button count", _
 my.computer.mouse.buttoncount)
myappds.mycomputer.addmycomputerrow("buttons swapped", _
 my.computer.mouse.buttonsswapped)
myappds.mycomputer.addmycomputerrow("mouse exists", _
 my.computer.mouse.exists.tostring())
myappds.mycomputer.addmycomputerrow("wheel scroll lines", _
 my.computer.mouse.wheelscrolllines.tostring())
myappds.mycomputer.addmycomputerrow("wheel exists", _
 my.computer.mouse.wheelexists)
myappds.mycomputer.addmycomputerrow("computer name", _
 my.computer.name)
myappds.mycomputer.addmycomputerrow("connection status", _
 my.computer.network.connectionstatus)
myappds.mycomputer.addmycomputerrow("connection status", _
 my.computer.network.ipaddresses(0).tostring())
 myappds.mycomputer.addmycomputerrow("operating system platform", _
 my.computer.operatingsystem.platform)
myappds.mycomputer.addmycomputerrow("version", _
 my.computer.operatingsystem.version.major & "." & _
 my.computer.operatingsystem.version.minor & "." & _
 my.computer.operatingsystem.version.revision & "." & _
 my.computer.operatingsystem.version.build)
myappds.mycomputer.addmycomputerrow("full name hkey_current_user", _
 my.computer.registry.currentuser.fullname)
myappds.mycomputer.addmycomputerrow("device name", _
 my.computer.screen.devicename)
這段代碼與控制my.application選項卡中的網(wǎng)格控件的代碼非常相似。用一個類型化的數(shù)據(jù)集來裝載my.computer對象屬性中獲得的值,并綁定到一個表格控件。
my.computer對象的元素不是都能夠顯示在表格控件里。下表列出的項目包括沒有在表格控件里顯示的屬性或方法。
my.computer
屬性/方法 描述
clock
 允許你訪問當(dāng)前中時區(qū)時間和本地時間,以及計數(shù)刻度。
 
eventlogs
 允許你訪問本地事件日志信息以及創(chuàng)建或刪除事件日志。
 
filesystem
 允許你進(jìn)行許多io操作,如復(fù)制文件、目錄,移動文件、目錄,讀取或?qū)懭胛募取R话闱闆r只需要一行代碼。
 
keyboard
 允許你獲取當(dāng)前鍵盤上某些案件的狀態(tài)。比如獲取ctrl鍵、shift鍵或atl鍵是否被按下,或者caps lock/scroll lock是否處于打開狀態(tài)等。
 
mouse
 允許你獲取鼠標(biāo)的狀態(tài)以及它所附帶的特定的硬件特征,如有幾個按鍵,是否帶有滾論等。
 
name
 告訴你當(dāng)前正運行的本地計算機的名稱。
 
network
 允許你訪問本地計算機的ip地址信息,本地計算機的連接狀態(tài),還有ping一個地址的功能。
 
operating system
 允許你訪問本地計算機平臺和版本的信息。
 
registry
 允許你訪問注冊表,并提供讀取和寫入注冊表的功能。
 
screen
 允許你訪問系統(tǒng)附帶的監(jiān)視器以及顯示屬性,如bitsperpixel和workingarea等。
 
my.computer在開發(fā)中的相關(guān)應(yīng)用
my.computer提供給你很多訪問系統(tǒng)底層的功能,你可以用在很多方面。比如你可以用network屬性和其相關(guān)的ping、pingwithdetails方法輕易地ping一個網(wǎng)絡(luò)地址。
dim pingresult as microsoft.visualbasic.net.pingresult = _
 my.computer.network.pingwithdetails("www.3leaf.com")
if pingresult.pingsuccesses > 0 then
 dim pingdetails as string = "average ping time in ms: " & _
 ingresult.averagepingtimeinmilliseconds & vbcrlf & _
 "bytes received: " & pingresult.bytesreceived.tostring() & vbcrlf & _
 "ip address: " & pingresult.ipaddress.tostring() & vbcrlf &_
 "ping attempts: " & pingresult.pingattempts.tostring() & vbcrlf & _
 "ping successes: " & pingresult.pingsuccesses.tostring()
 messagebox.show(pingdetails)
else
 messagebox.show("ping of www.3leaf.com was not successful")
end if
這段代碼最關(guān)鍵的部分是my.computer.network.pingwithdetails()。這行代碼返回一個pingresult對象,其中包含了標(biāo)準(zhǔn)ping動作所返回的數(shù)據(jù)。如果最少一次ping操作成功的話,平均響應(yīng)時間,獲得的字節(jié)數(shù),總ping數(shù)目和成功數(shù)目都會顯示在一個消息框中。my.computer.network還提供一個簡化版的ping,只返回true或false以示ping成功與否。像這樣ping一個地址的功能可以讓你方便地確定是否能與服務(wù)器正式建立通信。通過使用my.computer.network.connectionstatus,你還可以方便地確定當(dāng)前網(wǎng)絡(luò)連接的狀態(tài)。connectionstatus屬性根據(jù)計算機現(xiàn)在是否有網(wǎng)絡(luò)連接而返回true或false。
my.computer另一個很好的應(yīng)用是在需要訪問文件系統(tǒng)的時候。my.computer.filesystem提供給你更好的訪問文件系統(tǒng)的方法,所需的代碼比用vb.net來實現(xiàn)更少。用my.computer.filesystem,你可以只用一行代碼完成下列任務(wù)。
· 在文件后面附加文本。
· 從文件中讀取所有文本。
· 復(fù)制文件夾時創(chuàng)建所有完整路徑所需的父文件夾。
· 移動文件夾時創(chuàng)建所有完整路徑所需的父文件夾。
你還能用microsoft.visualbasic.fileio命名空間中的類型簡化文件夾、文件和驅(qū)動器的操作。這些類型一般都是my.computer.filesystem對象的屬性和方法返回的類型。
下面的例子是用my在文件末尾附加文本所需的全部代碼。
my.computer.filesystem.appendtext(filepath, “hello from " & _
 “my.computer.filesystem”)
如果你想讀取一個文件的全部文本,只需下列代碼即可做到。
my.computer.filesystem.readalltext(filepath)
復(fù)制和移動文件夾變得非常容易。
my.computer.filesystem.copyfolder(sourcepath, targetpath, true, true)
上面這行代碼將文件夾從sourcepath復(fù)制到targetpath。最后兩個布爾型參數(shù)指定targetpath是否要覆蓋已經(jīng)存在的文件夾以及指定是否需要創(chuàng)建所需的父文件夾。
剛才提到過filesystem對象的許多屬性和方法都可以返回drive、folder或file類型的對象。這些microsoft.visualbasic.fileio名字空間中的對象封裝了許多新的屬性和方法,可以讓一般的編程工作得到簡化。比如可以用一行代碼可以取得“我得文檔”文件夾所在的驅(qū)動器。
my.computer.filesystem.specialfolders.mydocuments.drive.drivename
更重要的是你可以獲得“我得文檔”文件夾所在的驅(qū)動器所剩余的磁盤空間。
my.computer.filesystem.specialfolders.mydocuments.drive.freespace
用文件對象對特定文件或文件夾的操作同樣非常容易。比如你可以用一行代碼輕松地重名名文件。
my.computer.filesystem.specialfolders.mydocuments.drive.freespace 
my.user
my名字空間的下一個功能塊是user對象。my.user對象允許你獲取當(dāng)前登錄用戶的信息如用戶名,域名,以及查詢他們所屬的組群。
圖 3
my.user 選項卡
控制表格控件的代碼如下:
myappds.myuser.addmyuserrow("display name", _ 
 my.user.displayname)
myappds.myuser.addmyuserrow("domain name", _
 my.user.domainname)
myappds.myuser.addmyuserrow("user name", _
 my.user.username)
myappds.myuser.addmyuserrow("windows role", _
 my.user.windowsroles(0).tostring())
這段代碼訪問了當(dāng)前用戶的信息,這非常簡單。你要做的所有事情就是訪問user對象的屬性。user對象是由下列屬性或方法組成的。
my.user
屬性/方法 描述
displayname
 允許你訪問當(dāng)前登錄用戶的顯示名稱。
 
domainname
 允許你訪問當(dāng)前登錄用戶所屬的域名,如果他屬于某個域的話。
 
isinrole
 允許你查詢用戶是否屬于一個特定的角色。
 
username
 允許你訪問當(dāng)前登錄用戶的用戶名。
 
windowsrole
 windowsrole是一個包含當(dāng)前登錄用戶帳號所屬的全部角色/組群信息的集合。
 
my.user在開發(fā)中的相關(guān)應(yīng)用
my.user能夠給你相當(dāng)多的關(guān)于當(dāng)前登錄用戶的信息。在很多方面,my所提供的user對象都是學(xué)習(xí)或使用.net framework的絕佳捷徑之一。
在以前版本的.net framework中,如果你想訪問類似my.user所提供的功能,你必須寫下面這么多代碼。
imports system.security.principal
imports system.threading.thread
…
dim winprin as windowsprincipal = thread.currentprincipal
messagebox.show(winprin.identity.name)
或者你得寫成這樣
messagebox.show(system.threading.thread.currentprincipal.identity.name)
但是,在使用了my之后就變得如此直觀。
my.user.username
這是一個用my簡化對.net framework操作的一個經(jīng)典例子。很多開發(fā)人員第一次面對framework時,不會本能地想到要從principal對象或從當(dāng)前線程取得進(jìn)程身份才能獲得當(dāng)前登錄的用戶的帳號名稱。很多開發(fā)人員以為這種信息肯定是在更高級的抽象層次上才能得到。現(xiàn)在用my,獲取這種信息既簡單又快速。
my.webservices和 my.forms
接下來你將要學(xué)到 my命名空間另外兩個主要功能,它們是my.webservices和my.forms。在進(jìn)入這一節(jié)之前,我們先做一個簡短的概述。
my.webservices讓你可以用和訪問數(shù)據(jù)源一樣簡單的語法訪問在項目中引用的web服務(wù)。
dgorders.datasource = _
 my.webservices.northwind.getorders().tables("orders")
如果用.net framework 1.0/1.1,要想做到上面那行代碼的功能,你必須創(chuàng)建一個webservice的實例。用這種my.webservices提供的功能可以快捷訪問所有添加到web引用的web服務(wù)。
最后一個重點my.form將帶回一種在visual basic桌面程序中出現(xiàn),并暫時消失在vb.net中的標(biāo)志性的編程風(fēng)格。如果你曾經(jīng)用vb6開發(fā)過,你肯定知道下面這種代碼,如果你的項目需要顯示某個窗體:
form2.show
在vb.net時代,你不能再用這種簡單的語法了。在vb.net中只能用下列代碼顯示一個窗體。
dim frm1 as new form1()
frm1.show()
使用my.forms,項目中所有的窗體都變成forms集合的成員。另外如果你想訪問某個窗體默認(rèn)的實例,你根本不需要使用my.forms。就是說下面的代碼是等價的。
my.forms.form2.show()
form2.show()
在接下來的章節(jié)里,你將學(xué)到更多用此方法顯示窗體的功能(譯注:可惜我們看不到了)。
如果你想看一些my.webservices和my.forms的例子,你可以看看示例程序相關(guān)的選項卡。所有這些工能將在本書的后邊詳細(xì)地敘述。(本文檔只是此書的樣章,所以到此為止了,想看后面請到亞馬孫書店購買此書)
結(jié)論
my命名空間對于一個開發(fā)人員來說主要有四項功能。首先它提供很多了以前版本的.net framework沒有的功能。第二,它提供了很多快速訪問.net framework的bcl的快捷方式。第三,在一定程度上,它讓你想問題時能將“計算機”類的詞當(dāng)作邏輯實體來操作。第四,它讓一些你熟悉的舊語法回歸到了visual basic中,比如form1.show。