Question Convert VBscript to VB.Net for compiling into single .EXE file

mjolnir

New member
Joined
Jan 20, 2011
Messages
1
Programming Experience
Beginner
Hi everyone,

I have a vbscript which I would like to compile into a single .EXE file. It's my understanding that you can't do this with vbscript and that the available applications which supposedly do the conversion are actually just glorified self extracting zip files which dump the vbs to a temp folder and run the script - not what I want.

I've no experience in VB.Net and very little in VBS so I'd appreciate it someone could look over the script and let me know what areas I need to change so I can investigate the correct syntax in VB.Net.

Firstly, is this possible? If so great! I guess it would be wise to explain what the script does just in case there might be an issue from the conversion. The script does several things in this order:
  • starts by scanning a CSV file (which contains a list of peoples usernames, their department and full name) looking for the line that contains a username match to that of logged in users's logon name via expanding the environment variable %USERNAME%
  • Once found, it splits the entire row into an array
  • Sets a series of Environment variables at Process level using contents of the array as values
  • Does a basic time check and sets the result as environment variable as above
  • Checks to see which version of CAD application is installed on users machine and starts application with command switch to process configuration file during startup

I don't think its overly complex now that I understand the VBS code but in VB.Net, I don't know what lines will work and what won't. I've not installed Visual Studio Express yet but will do so shortly - I presume this program is suitable for converting my script?

VB.NET:
Option Explicit

Const FOR_READING = 1 
Dim objFSO : Set objFSO = WScript.CreateObject("Scripting.FileSystemObject") 
Dim objWSHSHell : Set objWSHSHell = WScript.CreateObject("WScript.Shell") 
Dim objWSHNetwork : Set objWSHNetwork = WScript.CreateObject("Wscript.Network") 
Dim objCSV : Set objCSV = objFSO.OpenTextFile("L:\CADD_Standards\V8i_Workspace\Scripts\DRS_Groups.csv", FOR_READING, False) 
Dim strLoggedOnUser : strLoggedOnUser = UCase(objWSHNetwork.UserName) 
Dim blnFoundUser : blnFoundUser = False 
Dim strTimeOfDay

'check if its Morning, Afternoon or Evening
If Hour(Time)>=17 Then
  strTimeOfDay = "Evening"
ElseIf Hour(Time)>=12 Then
  strTimeOfDay = "Afternoon"
Else
  strTimeOfDay = "Morning"
End If

' Scan The CSV Index
Do Until objCSV.AtEndOfStream 
    Dim strLine : strLine = Trim(objCSV.ReadLine) 
    Dim arrSplit : arrSplit = Split(strLine, ",") 
    Dim strCSVUser : strCSVUser = Trim(Ucase(arrSplit(0)))
    Dim strCSVGroup : strCSVGroup = Trim(arrSplit(1))
	Dim strCSVName : strCSVName = Trim(arrSplit(2))
'If a match is found, set the variables and Start Microstation/Powerdraft	
	If strLoggedOnUser = strCSVUser Then 
        blnFoundUser = True 
        Call SetEnvVar("Process", "DRS_Group", strCSVGroup) 
        Call SetEnvVar("Process", "Users_Full_Name", strCSVName)
		Call SetEnvVar("Process", "Time_of_Day", strTimeOfDay)
		Call startMS()
		End If
Loop 
If blnFoundUser = False Then 
    MsgBox "User is not defined in CAD User Index", vbCritical + vbOkOnly, "Critical Error Occured" 
End If 
WScript.Quit

Sub startMS() 
Dim ms32bitpath : ms32bitpath = "C:\Program Files\Bentley\MicroStation V8i\MicroStation\ustation.exe"
Dim ms64bitpath : ms64bitpath = "C:\Program Files (x86)\Bentley\MicroStation V8i\MicroStation\ustation.exe"
Dim pwrdraft32bitpath : pwrdraft32bitpath = "C:\Program Files\Bentley\PowerDraft V8i\PowerDraft\draft.exe"
Dim ms32bit : ms32bit = Chr(34) & "C:\Program Files\Bentley\MicroStation V8i\" &_ 
                                          "MicroStation\ustation.exe" & Chr(34) &_ 
                                          "  -wcL:\CADD_Standards\V8i_Workspace\Startups\V8i_Startup.cfg"
Dim pwrdraft32bit : pwrdraft32bit = Chr(34) & "C:\Program Files\Bentley\PowerDraft V8i" &_ 
                                          "PowerDraft\draft.exe" & Chr(34) &_ 
                                          "  -wcL:\CADD_Standards\V8i_Workspace\Startups\V8i_Startup.cfg"
Dim ms64bit : ms64bit = Chr(34) & "C:\Program Files (x86)\Bentley\MicroStation V8i\" &_ 
                                          "MicroStation\ustation.exe" & Chr(34) &_ 
                                          "  -wcL:\CADD_Standards\V8i_Workspace\Startups\V8i_Startup.cfg"		  
If objFSO.FileExists (ms64bitpath) then
call objWSHShell.Run(ms64bit, 1, True)
ElseIf objFSO.FileExists (pwrdraft32bitpath) then
call objWSHShell.Run(pwrdraft32bit, 1, True)
ElseIf objFSO.FileExists (ms32bitpath) then
call objWSHShell.Run(ms32bit, 1, True)
Else MsgBox "Microstation/Powerdraft V8i is not installed correctly"
End If	
End Sub 

Sub SetEnvVar(var_scope, var_name, var_value) 
    Dim oEnv : Set oEnv = objWSHShell.Environment(var_scope)   
    oEnv(var_name) = var_value 
	End Sub

Feel free to ask me any questions if needed

Thanks for your time
 

Latest posts

Back
Top