Google Chrome command line link with named tags from a vb string - vb.net

I have a series of HTTP links stored in the database the have a named tag as part of the link (e.g. http://somecompany.com/products#123332) which represents a product 123332 in a rather large page.
The above link works fine when using IE but doesn't work with Chrome. The # character gets converted to %23 and Chrome can't find the ref. Changing the %23 back to # in the browser's address field works correctly.
The link is stored in a standard VB String and I've tried replacing the # with different values via String.Replace(). There must be a proper way to encode this correctly, I just can't seem to find any references on the web that seem to work.
A simplified example of the call would be something like:
Shell(strBrowser & strLink)
where strBrowser contains all the necessary path and exe name and the strLink is a string similar to what I mentioned above. Also as mentioned it works fine with IE. How can I go about encoding this correctly?
Thanks.
The code
If oFichierAide.LienIntern = 1 Then
strLink = " " + Chr(34) + VarGlobales.EmplacementAppl + oFichierAide.LienFichier + Chr(34)
Else
strLink = " " + Chr(34) + oFichierAide.LienFichier + Chr(34)
End If
Try
regBrowserKey = My.Computer.Registry.ClassesRoot.OpenSubKey("HTTP\shell\open\command", False)
strBrowser = regBrowserKey.GetValue(Nothing).ToString().ToLower().Replace(Chr(34), "")
strBrowser = strBrowser.Substring(0, strBrowser.LastIndexOf(".exe") + 4)
Finally
If regBrowserKey IsNot Nothing Then
regBrowserKey.Close()
End If
End Try
If strBrowser.Length > 0 And strLink.Length > 1 Then
Dim strCall As String = strBrowser + strLink
Shell(strCall, AppWinStyle.NormalFocus, False)
Return True
End If

You can open any chrome window to any URL stored with a string variable via Visual Basic by doing:
Dim appData As String = GetFolderPath(SpecialFolder.ApplicationData)
Dim chromelocation = appdata + "\Local\Google\Chrome\Application\chrome.exe"
process.start(chromelocation + strLink)
Chrome should then open up and the address it goes to should be the contents of strLink.

Related

How do I get only one result for each app instead of double?

Copy this into Visual Studio, add a textbox and it'll run.
Const NET_FW_ACTION_ALLOW = 1
Dim fwPolicy2 = CreateObject("HNetCfg.FwPolicy2")
Dim RulesObject = fwPolicy2.Rules
For Each rule In RulesObject
If rule.action = NET_FW_ACTION_ALLOW Then
TextBox1.Text += rule.name & vbnewline
End If
Next
This is an example of what I get but I only need each app to be listed once, not two times. What am I doing wrong or why does it behave like this?
qBittorrent
qBittorrent
Chrome
Chrome
Visual Studio
Visual Studio
and so on...
It behaves like this because rule.Name is not a unique identifier for a firewall rule. The same rule name may be used for different protocols (TCP, UDP), profiles (domain, private, public), direction (in, out), etc. If you are only interested in rule.Name, add them to a set, then print that set, as follows.
Const NET_FW_ACTION_ALLOW = 1
Dim fwPolicy2 = CreateObject("HNetCfg.FwPolicy2")
Dim RulesObject = fwPolicy2.Rules
Dim names As New HashSet(Of String)
' Create set of unique names.
For Each rule In fwPolicy2.Rules
If rule.action = NET_FW_ACTION_ALLOW Then
names.Add(rule.name)
End If
Next
' Add names to TextBox.
For Each name As String In names
TextBox1.Text += name & vbNewLine
Next
For Each rule In RulesObject
If rule.action = NET_FW_ACTION_ALLOW AndAlso TextBox1.Text.Contains(rule.name.ToString) = False Then
TextBox1.Text += rule.name & vbnewline
End If
Next
The above is one way to do it. It simply checks whether it's already added to the textbox. Btw, I don't know offhand whether or not rule.name is already a string so I added .ToString; if it's already a string, you don't need to add that.
Also, most of us would recommend using Option Strict, and declaring your variables as a type. i.e. Dim myVar as String = "some string"

how can a target the current user's documents folder?

So we have a system that requires users to log in and it has there own roaming profile. So in this string of code how can i target the current users document folder? (FYI excel 2010)
'WORKAROUND:
Dim PID As Double
Dim strRootPath As String
Const strExpExe = "explorer.exe"
Const strArg = " " '" /e,/root, "
'this is where i need to figure out how to target current user's documents
'// Change rootpath here
strRootPath = "C:\Data Files"
PID = Shell(strExpExe & strArg & strRootPath, 3)
the rest of the function does great... it opens file explorer i just cant figure the syntax for telling it to look for the current user.
Probably the best way would be with a function like this:
Function docsFolder() As String
docsFolder = CreateObject("WScript.Shell").SpecialFolders("MyDocuments")
End Function
There are other ways too but this one will work on any version of Windows and with user customizations.
For example, in my case, I have my documents folder on a mapped X: drive, so simply stuffing my username into a C:\ path would not work.
More Information:
Stack Overflow : Language independent way to get “My Documents” folder in VBA
Microsoft Technet : What is the path to My Documents?
MSDN : Wshell: SpecialFolders Property
I'm not sure how flexible you want this to be but you could try the following
strRootPath = "C:\Users\" + Environ("Username") + "\Documents"
Got it! thanks! for anyone that might care... The final string that made her run!
Function docsFolder() As String
docsFolder =
CreateObject("WScript.Shell").SpecialFolders("MyDocuments")
End Function
Private Sub test()
Dim PID As Double
Dim strRootPath As String
Const strExpExe = "explorer.exe"
Const strArg = " " '" /e,/root, "
'// Change rootpath here
strRootPath = "C:\Users\" + Environ("Username") + "\Documents"
PID = Shell(strExpExe & strArg & strRootPath, 3)
End Sub

Uploading File To Website

I am still a beginner with VBA, so if you need further explanation or I am not describing my problem correctly please let me know.
I am trying to achieve the following:
Upload a file from my computer to a website (You can only try to upload if you login, so sadly I cannot share a link)
In order to achieve this I need to do three things:
1) Click the "Upload" Button
2) Insert the Filepath into the search field of the PopUp Window
3) Click the "Open" Button
The website looks like this:
The PopUp Window looks like this:
The HTML code of the upload field is the following:
<div class="button-wrapper">
<input class="design-file-input" type="file">
<a class=" button prio1" href="javascript:void(0);">Design hochladen</a>
</div>
I guess there might be two solutions two my problem, however, I am not able to realize my plans.
IDEA 1
Somehow get the filepath into the input field and the page to download it
Therefore I tried the vba following VBA codes:
objIE.document.getElementsByClassName("design-file-input")(0).Value
objIE.document.getElementsByClassName("design-file-input")(0).innerText
And then try to somehow make the website submit my entry.
IDEA 2
Click the "Design Hochladen" Button.
objIE.document.getElementsByClassName("button-wrapper")(0).Click
But then the PopUp window comes up and I don't know how to control it with VBA
I am happy to hear and try your suggestions!! If you need any further details, just let me know! Thank you so much if you can give me any advice
Directly assigning the file path to the value of that specific HTML element does not work. A while ago, I had the same issue (automatically passing a file to an upload file dialog). After a long googling session, I found following solution. Unfortunately, I could not find the link from where I took this answer. In case I come accross the website, I will share the link with you:
Dim FilePath As String
Dim FileName As String
FilePath = Environ$("temp") & "\"
FileName = "test_file_for_upload" & ".xlsx"
UploadFile DestURL, FilePath & FileName, "file" 'Usage
'******************* upload - begin
'Upload file using input type=file
Public Sub UploadFile(DestURL As String, FileName As String, _
Optional ByVal FieldName As String = "File")
Dim sFormData As String, d As String
'Boundary of fields.
'Be sure this string is Not In the source file
Const Boundary As String = "---------------------------0123456789012"
'Get source file As a string.
sFormData = GetFile(FileName)
'Build source form with file contents
d = "--" + Boundary + vbCrLf
d = d + "Content-Disposition: form-data; name=""" + FieldName + """;"
d = d + " filename=""" + FileName + """" + vbCrLf
d = d + "Content-Type: application/upload" + vbCrLf + vbCrLf
d = d + sFormData
d = d + vbCrLf + "--" + Boundary + "--" + vbCrLf
'Post the data To the destination URL
IEPostStringRequest DestURL, d, Boundary
End Sub
'sends URL encoded form data To the URL using IE
Sub IEPostStringRequest(URL As String, FormData As String, Boundary As String)
'Create InternetExplorer
Dim WebBrowser: Set WebBrowser = CreateObject("InternetExplorer.Application")
'You can uncoment Next line To see form results
WebBrowser.Visible = True
'Send the form data To URL As POST request
Dim bFormData() As Byte
ReDim bFormData(Len(FormData) - 1)
bFormData = StrConv(FormData, vbFromUnicode)
WebBrowser.navigate URL, , , bFormData, _
"Content-Type: multipart/form-data; boundary=" + Boundary + vbCrLf
Do While WebBrowser.Busy
' Sleep 100
DoEvents
Loop
'WebBrowser.Quit
End Sub
'read binary file As a string value
Function GetFile(FileName As String) As String
Dim FileContents() As Byte, FileNumber As Integer
ReDim FileContents(FileLen(FileName) - 1)
FileNumber = FreeFile
Open FileName For Binary As FileNumber
Get FileNumber, , FileContents
Close FileNumber
GetFile = StrConv(FileContents, vbUnicode)
End Function
'******************* upload - end
The third argument "file" denotes the ID of the HTML element which needs to be triggered.
Hope this solution workds for you as well

The given path's format is not supported (VB.Net)

Below coding are code that I used to rename and store images at temporary files.
Declaration as below :
Dim extension As String = Path.GetExtension(hpf.FileName)
Dim rename As String = Now.ToString(gsUserStation + "ddMMyyHHmmss")
Dim imageRename As String = Path.Combine(rename + extension)
Dim imgUrl As String = Server.MapPath("source") & "\" & Path.Combine(rename + extension)
And code to save :
hpf.SaveAs(Server.MapPath("source") & "\" & imageRename)
This coding working fine but I don't know what happened when I run it sometimes with different code of station, it threw me an error
The given path's format is not supported.
When I do debugging, the variable rename (expectation result : BKI081116120002) became something like B+08:00I0811160002. I don't get it, how come the result of station code BKI became B+08:00I. So that is the one that causing the error to happen.
Any suggestion from you guys? Thank you.

Weird behavior of powercfg.exe when I execute it programatically

To programtically set display brightness I had two options :
Use PowerWriteACValueIndex and PowerWriteDCValueIndex API
Use PowerCfg.exe
Now, I tried both, and both gave me the same (weird) result. Here's the code I'm using :
With psiInfo
.FileName = "powercfg"
.UseShellExecute = True
.WindowStyle = ProcessWindowStyle.Hidden
.Arguments = "-setacvalueindex " & guidCurScheme.ToString() & " " & SUBGROUP_GUID.GUID_VIDEO_SUBGROUP & " " & SETTING_GUID.GUID_DISPBRIGHTNESS & " " & psValue.ValueAC
End With
pProc = Process.Start(psiInfo)
Where :
Public Const GUID_VIDEO_SUBGROUP As String = "7516b95f-f776-4464-8c53-06167f40cc99"
Public Const GUID_DISPBRIGHTNESS As String = "aded5e82-b909-4619-9949-f5d71dac0bcb"
Public Structure POWERSETTING
Public ValueAC As String
Public ValueDC As String
End Structure
Dim psValue As POWERSETTING
And guidCurScheme is the GUID of the active power scheme, as returned by PowerGetActiveScheme API function. I do the samething for DC part, with "-setdcvalueindex".
Now the weird behavior is as follow :
If I call this code in a button click handler event, it will set these values, but not the "general" screen brightness slider (and for that matter, the display brightness). (somebody edits the pictures links inside the post please)
If I the user clicks the button a second time, it will only then apply the setting.
If i put the code in a loop for it to run twice when the user click the button, it will still behave as in (1)
If I try to run the same command but manually from command-line, it will run work just fine.
This is totally unexpected, I don't know what I'm doing wrong. It gives the exact same result when I use the PowerWriteACValueIndex and PowerWriteDCValueIndex functions from Windows API.
maybe double-check the syntax of your argument by letting the app copy the string to clipboard. Sometimes just a tiny typo ruins it for me.
Also i'd check if the application that gets called is the one from syswow or from system32 (that depends on how you compile your app)
You can also check the exitcode or look at the error-output you get.
The error-output can be captured like this:
Dim so As System.IO.StreamReader
Dim se As System.IO.StreamReader
se = myproc.StandardError
so = myproc.StandardOutput
myproc.WaitForExit()
MsgBox(so.ReadToEnd & vbcrlf & se.ReadToEnd)