I have the following code to create a combo box that shows a list of ports in use on the PC What I want to do is take the users choice of port and assign it to a system variable using the SETX command. At the moment the user enters the port number manually in a batch file, the aim is to skip this step and replace it with the choice from the combo box
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
myPort = IO.Ports.SerialPort.GetPortNames()
ComboBox1.Items.AddRange(myPort)
The batch file looks like this at present and works fine
set /p port= "What is your port number "
setx port "%port%"
So is there a way to remove the user having to enter the port number and have the batch file run its script using the choice from the combo box Thanks
According to this post
For
set /p port= "What is your port number: "one can use the following in VB.NET:
where "COM5" is your port number (replace this with the value from your ComboBox).
For
setx port "%port%":Permanent environment variables are stored in the registry:
HKEY_CURRENT_USER\Environment.To set an environment variable in
HKEY_CURRENT_USER\Environment, do one of the following.Option 1:
where "COM5" is your port number (replace this with the value from your ComboBox).
Option 2:
Add Imports
SetUserEnvironmentVarReg:
Usage:
where "COM5" is your port number (replace this with the value from your ComboBox).
Let's create a test batch script. The test batch script will allow one command-line argument which will allow it to be used with the different options shown below. If a command-line argument is supplied, the batch script will used the specified value, otherwise it will use the value inherited from the environment variable.
TestEnvVar.bat:
One can use System.Diagnostics.Process to run the batch script.
RunProcessInheritEnvironmentVar:
Usage:
where "COM5" is your port number (replace this with the value from your ComboBox).
It's not clear whether or not you actually need to set the environment variable permanently (ie: in HKEY_CURRENT_USER\Environment). If the environment variable is only being used for the batch script being executed, one may consider one of the alternate methods shown below.
When using System.Diagnostics.Process an environment variable can be set using ProcessStartInfo.EnvironmentVariables which sets the environment variable within the scope of the
System.Diagnostics.Processprocess. The code below doesn't change the value in the registry.RunProcessSetEnvironmentVar :
Usage:
where "COM5" is your port number (replace this with the value from your ComboBox).
Lastly, let's look at how one could specify the port name as an argument because the test batch script allows command-line arguments. The code below doesn't change the value in the registry.
RunProcessWithArgument:
Usage
where "COM5" is your port number (replace this with the value from your ComboBox).
I've shown a variety of methods that can be used to specify an environment variable that can be used for a batch script depending upon your needs. For a slight variation of System.Diagnostics.Process usage see here.
Update:
When using System.Diagnostics.Process, one can also use StandardInput to provide value(s) for prompt(s).
TestEnvVar.bat:
RunProcessWithStandardInput:
Usage:
where "COM5" is your port number (replace this with the value from your ComboBox).
Additional Resources: