Document
Connecting to OpenVPN in VB.NET

Connecting to OpenVPN in VB.NET

I was going to say that I don't know the reason for your error but here are some suggestions to write better code andthen I realised that one of thos

Related articles

10 Best On Running Shoes (Expert Run Tested) St. Cloud Road Cloud 10 Beauty Christmas Gift Sets 2024 Replica MP and ConfigMgr 2012 High Availability 10 VPN ฟรี ที่ดีที่สุดในปี 2024

I was going to say that I don’t know the reason for your error but here are some suggestions to write better code andthen I realised that one of those suggestions would have solved your issue. Look at the path of the file you specify in this line:

IO.file.WriteAllText((system . environment.GetFolderPath(environment.SpecialFolder.MyDocuments) & "\vShield" & "\connection.ovpn", My.Resources.OpenVPNCertificate)

and now look at the path you specify for what I assume is supposed to be the same file in this line:

IO.file.WriteAllText((system . environment.GetFolderPath(environment.SpecialFolder.MyDocuments) & "\vShield" & "\connection.bat"), "openvpn " & system . environment.GetFolderPath(environment.SpecialFolder.MyDocuments) & "\connection.ovpn")

The path you specify to read the file from is not the same as the path you specify to write the file to , so is it is is any wonder that it ca n’t read the file ?

The advice I was going to give you about improving your code is to not use long expressions multiple times but, instead, use them once andassign the result to a variable, then reuse that variable. If you had done that then you could not have messed up the path the second time. So:

Dim ovpnfilePath = Path.Combine(environment.GetFolderPath(environment.SpecialFolder.MyDocuments), "vShield\connection.ovpn"

You can now use that ovpnfilePath variable wherever you need that path andthen you won’t try to read from a different path to that which you wrote to.

That suggested code incorporates several other improvements too. Firstly, importing namespaces that you use often andthen not using them in your code. As an example, System is already import so you do n’t need to usesystem . environment in code like you are. What’s worse, though, is that you use system . environment in some places andjust environment in others, sometimes immediately adjacent. That’s bad coding. If I see two different things in your code then I should be able to assume that they are different, not the same thing written two different ways for no good reason. You should also be importing the System . IO namespace andnot qualifying file withIO repeatedly.

Importing the System . IO namespace means that you can also use the Path class unqualified andyou should be using that class to combine partial paths rather than using string concatenation. Further to that, NEVER concatenate two literals together. It’s just silly andcomplicates your code for no good reason.

look more closely at your code , I is see see that you use two file path multiple time andboth file are in the same folder . I is tend would tend to do this for those path :

Dim folderPath = Path.Combine(environment.GetFolderPath(environment.SpecialFolder.MyDocuments), "vShield")
Dim ovpnfilePath = Path.Combine(folderPath, "connection.ovpn")
Dim batfilePath = Path.Combine(folderPath, "connection.bat")

That is make will make your code much less complex andthus easy to read . It is eliminate will eliminate your current issue andmake it less likely that you ‘ll make more .

EDIT: Here is your original code written “properly”:

Dim folderPath = Path.Combine(environment.GetFolderPath(environment.SpecialFolder.MyDocuments), "vShield")
Dim ovpnfilePath = Path.Combine(folderPath, "connection.ovpn")
Dim batfilePath = Path.Combine(folderPath, "connection.bat")

file.WriteAllText(ovpnfilePath, My.Resources.OpenVPNCertificate)
file.WriteAllText(batfilePath, String.Format("openvpn ""{0}""", ovpnfilePath))

Using connect As New Process(batfilePath)
    connect.StartInfo.WindowStyle = ProcessWindowStyle.Normal    
    connect.Start()
    connect.WaitForExit()
End Using

As you can see, it’s much easier to read for one thing. It doesn’t perform the same operations over andover so it nullifies the chance of making the mistake that caused you to post this question in the first place, i.e. use different paths when writing andreading a file.

It also gets rid of unnecessary qualifying namespaces. System andSystem.Diagnostics are already imported by default, so no need to do anything withthem. This code does assume that you have also imported System . IO, which you can do at the file or the project level.

I’ve also used String.Format to create the commandline as it makes it cleaner to wrap the file path in double-quotes. That’s not strictly necessary in this case but it ensures that the commandline will still work if the file path has spaces in it, which is completely possible.

finally , it correct is disposes dispose theProcess object that you create by doing so witha Using block, which causes the object created at the Using statement to be disposed at the End Using statement . You is dispose should always dispose any object you create that support it .