首页 > 编程 > .NET > 正文

Retrieving Environmental Details using VB.NET

2024-07-10 13:08:01
字体:
来源:转载
供稿:网友
注册会员,创建你的web开发资料库,windows based application would generally have requirement to access the windows system directory path, current directory , windows directory or to retrieve the current computer name,username, domainname, osversion etc. retrieving these information required api calls in vb6.

vb.net removes the complexity of calling api to access such useful information by providing the class library environment as part of .net. as known to all of us, the class library is accessible to all the .net languages.

here is an example that retrieves the above information using vb.net.

1) place a button in the form
2) place a textbox in the form and name it as txtinfo
3) set the multi-line property of the textbox(txtinfo) to true
4) import the system.environment namespace
5) write the following code in the commandbutton click event

        dim s as string
        dim o as system.environment

        s = "current directory=system.environment.currentdirectory()->"
        s = s & o.currentdirectory() & vbcrlf

        s = s & "commandline=system.environment.commandline()->"
        s = s & o.commandline() & vbcrlf

        s = s & "environment variable=system.environment.getenvironmentvariable(variable)->"
        s = s & o.getenvironmentvariable("path") & vbcrlf

        s = s & "machinename=system.environment.machinename->"
        s = s & o.machinename & vbcrlf

        s = s & "systemdirectory=system.environment.systemdirectory->"
        s = s & o.systemdirectory & vbcrlf

        s = s & "username=system.environment.username->"
        s = s & o.username & vbcrlf

        s = s & "userdomainname=system.environment.userdomainname->"
        s = s & o.userdomainname & vbcrlf

        s = s & "osversion=system.environment.osversion->"
        s = s & o.osversion.tostring & vbcrlf

  txtinfo.text = s

the namespace system.environment provides methods  to access various information like system name, domain name , username, osversion etc. these methods returns a string as a parameter. values are retrieved and stored in the local variable s. to avoid using a length qualifier system.environment with each call, a varaible is declared of type system.environment and the same is substituted there.

when the button is pressed, all the above information  are retrieved and posted in the textbox txtinfo.

happy programming with vb.net.



发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表