8 Ways to Get To Know Your User’s Computer
August 10th, 2007
Often you’ll find in your programs that you need to get information about the computer it’s running on. Fortunately, the .NET Framework makes it easy to get a lot of this information and the My namespace in VB.NET can make it even easier. Here are 8 key areas where you can get the info you need for your programs.
1. Memory
The My namespace provides four functions to retrieve the amount of available and total physical and virtual memory on a system. This can be important if you’re tracking memory related error issues or need to track available memory for other reasons. Here’s an example of these functions in action
Dim VirtualMemoryUsed As ULong = My.Computer.Info.TotalVirtualMemory - _
My.Computer.Info.AvailableVirtualMemory
Dim PhysicalMemoryUsed As ULong = My.Computer.Info.TotalPhysicalMemory - _
My.Computer.Info.AvailablePhysicalMemory
.
2. Windows Version
As I pointed out in my previous article, 10 Things to Avoid When Moving From VB6 to VB.NET, it is easy to get the Windows version using the My namespace, like so:
lblWindowsVersion.Text = String.Concat(My.Computer.Info.OSFullName, " ", My.Computer.Info.OSVersion) .
These functions can be quite useful for support purposes and to detect Windows patch levels on users’ systems.
3. Computer Name
The My.Computer.Name function simply returns a string with the current system’s name. I’ve found this very useful in situations where I have users who don’t have a set seating arrangement, such as in a call center, for tracking and audit purposes.
4. Mouse
If you have a special use for a mouse scroll wheel in your application, it’s important to know if the user has a scroll wheel or not and what their scroll lines setting is. My.Computer.Mouse makes this easy:
If My.Computer.Mouse.WheelExists Then
ScrollValue = My.Computer.Mouse.WheelScrollLines
Else
ScrollValue = 0
End If
5. UI Culture
As applications become more and more internationalized, developers often find that they have to take the user’s culture settings into account when formatting strings, numbers and dates. To retrieve this information, do the following:
Dim CurrentCulture As System.Globalization.CultureInfo = My.Computer.Info.InstalledUICulture .
6. Internet Connection
How can you tell if the client app has a connection to the Internet? There are a number of ways to do this, some complex, some easy, some reliable, some not so reliable. One method I like is to ping the site(s) that my application will need to access.
If My.Computer.Network.Ping("www.mywebservicehost.com") Then
'Internet should be available
End If
Of course, this might not work depending of firewalls and other security measures on both the client and server ends.
7. IP Address
To get this information, we’ll have to use the System.Net.Dns namespace and the GetHostEntry function. Here’s what it looks like:
Dim CurrentIP As String = System.Net.Dns.GetHostEntry(My.Computer.Name).AddressList(0).ToString .
8. Current User
We often need to know who the current user is on the system and what their rights are. The My.User functions make this quite easy. My.User.Name gets the currently logged in user’s name. If we wanted to have separate settings for administrative users vs. regular users, we could write code like this:
If My.User.IsInRole(ApplicationServices.BuiltInRole.Administrator) Then
'do admin stuff
Else
'do regular user stuff
End If
Sometimes a user might be running under an alias. This can happen if the user is running a program with elevated permissions and your program is shelled out to from it. In this case, you can use a function like this to discover the logged in user by finding the owner of the Windows shell program, explorer.exe.
Public Function GetActualUserName() As String
Dim ActualUserName As String = ""
Dim CurrentProcesses As Management.ManagementObjectCollection
Dim ProcessSearch As Management.ManagementObjectSearcher
Dim ProcessItem As Management.ManagementObject
ProcessSearch = New Management.ManagementObjectSearcher("Select * from Win32_Process")
CurrentProcesses = ProcessSearch.Get
For Each ProcessItem In CurrentProcesses
Dim ProcessOwner(2) As String
ProcessItem.InvokeMethod("GetOwner", ProcessOwner)
If (ProcessItem("Name").ToString = "explorer.exe") Then
ActualUserName = ProcessOwner(0).ToString
Exit For
End If
Next
Return ActualUserName
End Function
I hope you found these tips helpful. If you would like further information on any of these items or if you have a question or additional information about any of these that you would like to share, please leave a comment.
Entry Filed under: Tip Sheets
Rate This Article:









(1 votes, average: 4 out of 5)
1 Comment Add your own
1. Dj Snake | March 5th, 2008 at 10:46 am
Hay dos formas mucho mas faciles de hacerlo:
Public Function GetActualUserName() As String
Return System.Environment.UserName
End Function
Public Function GetComputerName() As String
Return SystemInformation.ComputerName
End Function
Saludos!
Leave a Comment
Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>
Trackback this post | Subscribe to the comments via RSS Feed