html5中文学习网

您的位置: 首页 > 网络编程 > ASP.NET » 正文

Retrieving Environmental Details using VB.NET_.NET教程_编程技术

[ ] 已经帮助:人解决问题
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.



f4KHTML5中文学习网 - HTML5先行者学习网
f4KHTML5中文学习网 - HTML5先行者学习网
(责任编辑:)
推荐书籍
推荐资讯
关于HTML5先行者 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 人才招聘 - 帮助