Click here to Skip to main content
15,884,388 members
Articles / Programming Languages / VBScript

How to Get Windows Directory, Computer Name and System Information Details

Rate me:
Please Sign up or sign in to vote.
2.16/5 (16 votes)
17 May 2004CPOL 232.4K   4.2K   18   9
This small program demonstrates how to get the Windows directory path name, Computer Name, and System information details
Sample Image - SystemUtility.jpg

Introduction

The utility helps you to understand how to return computer system name/Windows system path and system information from a Windows system (95, 98, NT and XP).

System Library Files

You have to include the following system DLL files into your application.

VB.NET
Private Declare Function GetComputerName Lib "kernel32" _
    Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Private Declare Function GetWindowsDirectory Lib "kernel32.dll" _
    Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long

GetTheComputerName Function

The following GetTheComputerName function returns the computer name from your system. See how the code works with System DLL file.

VB.NET
Public Function GetTheComputerName() As String

    Dim strComputerName As String ' Variable to return the path of computer name
    
    strComputerName = Space(250)  ' Initialize the buffer to receive the string
    GetComputerName strComputerName, Len(strComputerName)
    strComputerName = Mid(Trim$(strComputerName), 1, Len(Trim$(strComputerName)) - 1)
    GetTheComputerName = strComputerName

End Function

GetTheWindowsDirectory Function

The following GetTheWindowsDirectory function returns the Windows system path from your system. See how the code works with System DLL file.

VB.NET
Public Function GetTheWindowsDirectory() As String 
    
    Dim strWindowsDir As String        ' Variable to return the path of Windows Directory
    Dim lngWindowsDirLength As Long    ' Variable to return the length of the path
    
    strWindowsDir = Space(250)         ' Initialize the buffer to receive the string
    lngWindowsDirLength = GetWindowsDirectory
	(strWindowsDir, 250) ' Read the path of the windows directory
    strWindowsDir = Left(strWindowsDir, 
	lngWindowsDirLength) ' Extract the windows path from the buffer
    
    GetTheWindowsDirectory = strWindowsDir 

Displaying the System Information

The following lines of code will display all the system environment variables in a textbox.

VB.NET
Dim intInc As Integer
Dim strDisplay As String
    
Text1 = ""
' Here we start printing
For intInc = 1 To 35
    strDisplay = strDisplay & Environ(intInc)
    strDisplay = strDisplay & Space(5)
Next intInc
    
Text1 = strDisplay

Conclusion

This small program demonstrates how to call the system functions from the Windows DLL file and implement into your system. From this program, you found how to call GetComputerName and GetWindowsSystemDirectoryPath information. Also, you identified the importance of System information using Environ variables. I hope this will help you if you don't have any prior knowledge of system handle functions.

History

  • 17th May, 2004: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
Binoy is a software developer of Information Technology Division in Memphis,TN.

Comments and Discussions

 
GeneralMy vote of 1 Pin
Yazeed Hamdan12-Sep-09 13:16
Yazeed Hamdan12-Sep-09 13:16 
GeneralPrinter Utilities Pin
ThangaDharma6-Oct-05 7:01
ThangaDharma6-Oct-05 7:01 
GeneralPrinter Utilities Pin
ThangaDharma6-Oct-05 7:00
ThangaDharma6-Oct-05 7:00 
QuestionWhy? Pin
28-Mar-05 15:16
suss28-Mar-05 15:16 
GeneralVbscript get Computer Name and Windows Path name Pin
kornniwat@gmail.com22-Mar-05 22:11
susskornniwat@gmail.com22-Mar-05 22:11 
GeneralWhy not.. Pin
Klaus Weisser18-May-04 6:10
Klaus Weisser18-May-04 6:10 
using the .NET Framework class Environment ?
This class provides all the functionality your article is about.
GeneralRe: Why not.. Pin
eagleboy18-May-04 13:48
eagleboy18-May-04 13:48 
GeneralRe: Why not.. Pin
Klaus Weisser18-May-04 19:53
Klaus Weisser18-May-04 19:53 
GeneralRe: Why not.. Pin
Anonymous24-Sep-04 4:44
Anonymous24-Sep-04 4:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.