How to read VB formatted code from a text file? - vb.net

I writing a pair of programs in visual basics that will consist of a client and receiver. The client is completed making an output text file similar to below.
Dim FileName As String = "Text.txt"
Dim message As String
Dim file As System.IO.StreamWriter
'lblmessage.text says "Call MsgBox("Hello!", 0, "Version Beta 0.3")"
lblmessage.text = message
Dim Drive As String = "C:\hello world\" & FileName
file = My.Computer.FileSystem.OpenTextFileWriter(Drive, True)
file.WriteLine(message)
file.Close()
A sister program that is designed to be a reader will read the generated file.
The program will then take the text located in the selected file and use it as code in the readers programming.
Best example I can show...
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim fileReader As String
fileReader = My.Computer.FileSystem.ReadAllText("C:\hello world\Text.txt")
fileReader
End Sub
where "fileReader" is suppose to run the generated code from the previous program and use it as code it the Reader.
The point of this program is to create help tickets in the client and have a tool for reviewing these ticket in the same way they were submitted through the second app.

vba and vb.net is different. I'm not sure in which language your doing your stuff.
vba: use the ScriptControl.Eval command to execute a bunch of commands.
vb.net: it's a bit more code. you can use VBCodeProvider Class. s this SO Question: Load VB.net code from .txt file and execute it on fly using System.CodeDom.Compiler
UPDATE
I found a perfekt resource if you are interested in how to do it right and how it works in the background: http://www.codemag.com/article/0211081

Related

opening the Windows Print Pictures dialog using VB.Net

I want to call the Windows Print Pictures dialog using VB.net. the one Windows comes up with when you go to print a picture, the one that lets you print full page, 8X10, 4X6, 5X7, contact sheet, etc.
I've tried using printdocument and printdialog but those only give you the standard print dialog window for documents.
I've searched Google and all the results were completely unrelated to the Print Pictures dialog and therefore completely useless.
I've seen the same question for C+ here
Print image using windows print image dialog
using CLSID_PrintPhotosDropTarget COM object.
but this doesn't really help me since I'm using VB.net (Visual Studio 2008 to be precise) but I'm including it here as it has a screenshot of the dialog I am trying to call.
any help would be appreciated.
Okay, just this once, I will translate for you but you need to find an on line converter that you like. Many examples are available in C#. Also please take the tour to learn how to ask a proper question.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'The C# code
'String fileName = #"C:\Development\myImage.tif";//pass in Or whatever you need
'var p = New Process();
'p.StartInfo.FileName = fileName;
'p.StartInfo.Verb = "Print";
'p.Start();
Dim fileName = "C:\Users\xxxx\Desktop\Graphics\Baby owl.jpg"
Dim p = New Process
p.StartInfo.FileName = fileName
p.StartInfo.Verb = "Print"
p.Start()
End Sub

HTML Agility Pack Causes Code to Stop Executing After Load Call

I am creating a small application to parse HTML of a page into variables so I can generate code for another proprietary application. I am using VB with HTMLAgilityPack for parsing. When I execute the load statement, no error are shown but all other code after that line simply fails to execute, as if it isn't even there.
Private Sub Importer_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim strImportType As String
Dim strImportURL As String
strImportType = main.importType
strImportURL = main.importURL
Dim web As New HtmlAgilityPack.HtmlWeb
Dim content As New HtmlAgilityPack.HtmlDocument
content = web.Load(strImportURL)
After using Visual Vincent suggestion to open the Exceptions Settings and force VB to fail it allowed for more information. This issue was simply that the file was not being found due to spaces in the filename. Adding quotes around the filename resolved the issue.

Using Visual basic 2017 to navigate to a esp8266 wifi switch (Sonoff)

I am using the below code to navigate to a specific web address as follows :
WebBrowser1.Navigate("http://192.168.0.157/cm?cmnd=POWER%20Toggle"
The fact is that the the link returns a .json file and the WebBrowser controls displays the default save file dialog asking if i want to save the file or run it.
I want to ignore it the dialog and read from the .json file directly(i mean after downloading it).
I just want to get rid of the Save dialog of the webbrowser.I am a newbie so i don't know what to search or how to ask properly.
Though your post is not even close to be standard and hardly explains the issue, what i understand so far is that you have a few issues and i will answer them separately.
Disabling the download dialog of the webbrowser and downloading the files automatically
Firstly, you mentioned it returns a .json file. So , you can easily add a SaveFileDialogto your form or set a custom path(maybe in a variable) and check if the webbrowser is trying to download any .json files. Then you will Cancel the call(typically i mean that cancel the popup that says Save , Run ...) and make use of the SaveFileDialog or the local variable to save the file directly to disk. Here's a sample which uses a local string variable as the path and saves the .json file directly to disk :
Imports System.ComponentModel
...
Dim filepath As String '''class lever variable
Private Sub myBroswer_Navigating(sender as Object, e As WebBrowserNavigatingEventArgs) Handles myBroswer.Navigating
If e.Url.Segments(e.Url.Segments.Length - 1).EndsWith(".json") Then
e.Cancel = True
filepath = "C:\test\" + e.Url.Segments(e.Url.Segments.Length - 1)
Dim client As WebClient = New WebClient()
AddHandler client.DownloadFileCompleted , AddressOf New AsyncCompletedEventHandler(DisplayJson);
client.DownloadFileAsync(e.Url, filepath)
End If
End Sub
Displaying the result AKA .json
It is very easy to de-serialize/parse .json files.But first, download this , extract the ZIP and Add Reference to Newtonsoft.Json.dll. Now consider the bellow code snippet :
Private Sub DisplayJson()
Dim parseJson = Newtonsoft.Json.Linq.JObject.Parse(File.ReadAllLines(filepath))
MsgBox(parseJson("element name here").ToString)
End sub
Hope this helps

Cannot set OpenFileDialog initial directory to Downloads in visual basic [duplicate]

This question already has an answer here:
How to reference the current Windows user's video folder path in VB.net
(1 answer)
Closed 6 years ago.
I am trying to set the initial directory to the downloads folder but it doesn't work, Even though it's a perfectly valid path. Here is the code that I am using:
Private Sub btn_AddMod_Click(sender As Object, e As EventArgs) Handles btn_AddMod.Click 'This brings up the file dailoge
Dim Downloads As String = "\Downloads" 'A variables called \Downloads
Dim UserprofilePath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) 'This finds the directory to the User profile environment variable
Dim Downloadspath As String = UserprofilePath.Insert(0, "") + Downloads 'This adds \downloads to userpath
OpenFileDialog1.InitialDirectory = Downloadspath 'This sets the Open File Dialog to go to the users downloads
txt_setmodname.Text = Downloadspath 'This is used for debugging, it sets a textbox to the path
OpenFileDialog1.ShowDialog() 'This opens the Dialog
End Sub
When I copy the output text, the path is perfectly valid but instead of taking me to the path, it takes me to MyDocuments
That's some wacky code you have there. I'm not sure why it doesn't work and I'm not too interested in finding out. I just tested this and it worked as you want:
Using ofd As New OpenFileDialog
ofd.InitialDirectory = IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Downloads")
ofd.ShowDialog()
End Using
Obviously you can use an OpenFielDialog that you created in the designer instead of created in code if you want.
By the way, it should be noted that the user's Downloads folder is not necessarily in that location. Mine is on my D: drive, while my personal folder is on my C: drive. For people who keep C: only for system files, all their libraries and the like may be on a secondary drive. Unfortunately, there's no easy way to get the path for the Downloads folder like there is for Documents and some others. I'm guessing that the path is stored in the Registry or the like, but I'm not sure where.
I looked further into it and found out that there is a registry entry for the downloads path, so I used that instead and that seemed to have worked, My code is as follows.
Private Sub btn_AddMod_Click(sender As Object, e As EventArgs) Handles btn_AddMod.Click
Using ofd As New OpenFileDialog
Dim DownloadsPath = My.Computer.Registry.GetValue(
"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\", "{374DE290-123F-4565-9164-39C4925E467B}", Nothing)
ofd.InitialDirectory = DownloadsPath
ofd.ShowDialog()
End Using
I'm not sure why the other method didn't work, it always took me to the MyDocuments folder for some reason.

Get file from "Windows Explorer - Open With"

I'm not quite sure where to start with this. On right-clicking on a generic file in Windows Explorer (e.g. *.doc for a Word document) one can choose "Open with...". I 'd like to know how the program knows what file has been "passed" (is that the right word?). Is it done via arguments? How can I implement this in my own application?
I tried manually adding a file path to the arguments of one of my applications when it is run, but the path includes spaces (which denotes a new argument). How does Windows get round this/what do I need to do to solve this?
Regards,
Robbie
To retrieve the arguments used from the command line:
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Dim sMsg As String = ""
For Each sArg As String In My.Application.CommandLineArgs
sMsg &= sArg & ": "
Next
MsgBox(sMsg)
End Sub
Place the code in the WinForm _Load, Console Main, etc.
If the above was run with: Hello World as the command line Hello: World: would display.
Here is some VB code to open a file:
Case Keys.F4
Process.Start("H:\OIS\PROCEDUR\OIS8ProcedureManual.doc")
In this case Windows looks up .doc in the file types and uses the .doc entry to run Word and pass it the filename.
Process.Start has a second parameter that contains Arguments so you could provide a path to an .exe in the first param and the argument(s) in the second. Actually there are 5 signatures for Process.start. The most powerful ones uses the ProcessStartInfo class to provide you with the most control.
Post the code you wrote for the second group of questions if the above didn't help.