VB: Download ZIP file from internet (and extract) - vb.net

I need to somehow download a ZIP file from the internet using Visual Basic.
Here's what I currently have:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim NewAppData As String
NewAppData = Environment.ExpandEnvironmentVariables("%AppData%/.minecraft.zip")
Call DownloadFile("http://blahblahbacksheep.co.cc/.minecraft.zip", NewAppData)
End Sub
When I debug the program it gives me the following:
Function 'DownloadFile' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used.
What I'm looking to do is download the ZIP file and then extract it. And if possible, show label's with percentage for example:
Downloaded: 100%
Extracting: 35%....
Could anyone give me any resources or write up a bit of code to do some/all of this?
Thanks

For the unzipping part of your question: 7-Zip is a really great, open source, file archiving utility that has a nice command-line interface. Here's an example of how to call 7-Zip from VB (once it's been installed):
Set WshShell = VBA.CreateObject("WScript.Shell")
WshShell.Run "c:\Program Files\7-Zip\7z.exe " & _
"a -tzip myarchive.zip file.dat file2.txt file3.png", 1, True
This example compresses three files into an archive, while you want to extract files from an archive... just look up the appropriate command for that in the documentation.
Note that a command window will pop up while 7-Zip is executing, and in there you'll see a % progress indicator. If you want this indicator to appear somewhere else, then with a bit of ingenuity you can probably pipe the standard output through to some other relevant location.

Related

Can't delete .xlsx file - Next Step?

I'm getting an IOException (file in use by another process) error when trying to delete an .xlsx file. I moved the delete code to the front of Form_Load() just to make absolutely certain that nothing else I was doing was trying to open or read it.
Process Explorer can't find any references to the file to see what else might be trying to use it.
The file properties indicate that the user and system have full control.
I don't know what to look at next.
Private Sub PreEdit2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
My.Computer.FileSystem.DeleteFile("\\HOSTNAME\Folder\Import Data\Other Import Files\" & "9710.xlsx")
I tried a local reference to the file just for good measure.
My.Computer.FileSystem.DeleteFile("C:\Folder\Import Data\Other Import Files\" & "9710.xlsx")
Really stumped here. Any suggestions sincerely appreciated.
EDIT:
Modified my program to delete all kinds of other files in the same directory. They delete with no problem.
Created an empty test program with one line that tries to delete the file
and it gets the same IOException.
So, my program isn't doing it. The error says something else is using the file, no monitoring program I have used shows the any process involved with the file, and File Explorer can delete it? Strange things are afoot at the Circle K.
EDIT 2:
I have moved the file and tried deleting it in lots of different directories. I can delete it everywhere else I try it. The only place I don't seem to be able to delete it (with my new single purpose test program) is in the directory it was originally copied to. It should be remembered that all of the other files in that directory I can use the program to delete with no problem.

Save and open vbs script programmatically

I have searched a lot on this method but no way
I want my VB Program as once opened it gives user some messages using vbs script file
so once program is opened the vbs file saved in temp and be ran to say the message to user
i have used this code with the vbs file imported to Resources
but unfortunately it works only with one line script not script with many lines
Dim Variable As String = Environ("temp") & "Message.vbs"
If Not System.IO.File.Exists(Variable) Then
System.IO.File.WriteAllBytes(Variable, My.Resources.Message)
End If
Process.Start(Variable)
i have used this second code as well but it gives error in compilation because of Save
Dim Variable As String = Environ("temp") & "\Message.vbs"
IO.File.Delete(Variable)
My.Resources.Access.Save(Variable )
Shell("explorer" & Variable )
Please Help me with this code, i have spend a long time to get solution but nothing
Thanks in advance
I made some minor changes to your first example and created a simple vbs file. The first change is because a VB script file is a just a text file I added the resource as a text file and changed the WriteAllBytes statement to WriteAllText. Second, because WriteAllText, (and WriteAllBytes also), overwrite the file if it already exists I eliminated the If statement. You may still want that, in case you really do not want to overwrite the file. Finally, I added a backslash to the beginning of the filename to create the file in the temp folder. Otherwise you get drive:\temppath\tempMessage.vbs". This executed my simple script file just fine.
.NET code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Variable As String = Environ("temp") & "\Message.vbs"
System.IO.File.WriteAllText(Variable, My.Resources.hello)
Process.Start(Variable)
End Sub
My VBScript file:
MsgBox "Hello World", 0,"Messagebox #1"
MsgBox "My name is Fred", 0,"Messagebox #2"
MsgBox "I have to go now", 0,"Messagebox #3"

Vb.net How could I hide mp3 files in a external file (.bin or other?) and then get this file [ opening with AxWMP ]

Simply I just want to make it harder to get the mp3 files of the program. Can they be contained in 1 directory that will be harder to access? I don't want it be attached into the program to avoid large .exe file better are some external files /songs.(extension?) .
If its possible tell me
->How could I compress this files in 1 directory(extension) and how to get this file later via vb to play it via vb
How I get a file into the player.
AxWindowsMediaPlayer1.URL = mediaList(ListBox1.SelectedIndex)
mediaList what it does.
Dim mediaList As New List(Of String)
Private Sub Button1_Click(sender As Object, e As EventArgs)
OpenFileDialog1.Multiselect = True
OpenFileDialog1.ShowDialog()
For Each s As String In OpenFileDialog1.FileNames
mediaList.Add(s)
ListBox1.Items.Add(System.IO.Path.GetFileNameWithoutExtension(s))
Next
Me.ListBox1.SelectedIndex = Me.ListBox1.SelectedIndex + 1
End Sub
Please give me some hints
You could probably just add a few dummy bytes to the beginning of the file so they aren't recognized as MP3. Then, when you go to play your "altered" MP3 files, take those dummy bytes out and play the "original" file.

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.

Strange Error When Using Tesseract in VB.net

I have the current code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Bitmap As New Bitmap("image.png")
Dim ocr As tessnet2.Tesseract = New tessnet2.Tesseract()
ocr.SetVariable("tessedit_char_whitelit", "0123456789")
ocr.Init("c:\", "fra", False)
Dim result As List(Of tessnet2.Word) = ocr.DoOCR(Bitmap, Rectangle.Empty)
For Each word As tessnet2.Word In result
RichTextBox1.Text &= word.Text & "(" & word.Confidence & ") "
Next
End Sub
I just have a normal RichTextBox and a button on the form. I also have an image in the debug directory called "image.png".
Every time I run this, the program just closes. I did a step through and all of a sudden a file locater came up asking for "tessnet2.cpp"
I have a reference to the dll. I also don't know what the ocr.Init(...) line is for.
Any help would be nice!
First of all, thank you very much for your simple but effective code. After 3 days search I found this code for VB (not VC). Of course I copied and pasted it immediately and the same problem occured for me, too. Then:
I uninstalled Tesseract 3.xx
Checked RegEdit for Tesseract 3.xx and deleted them (whoever want to do this step; please be careful not to destroy anything)
Copied tessdll.dll in the same folder.
The main problem is:
ocr.Init("c:\", "fra", False) it should be something like this:
ocr.Init("c:\tessdata", "fra", False) in fact my real line is:
ocr.Init(Application.StartupPath & "\tessdata", "eng", False)
Noticed that in the folder "...\Visual Studio 2008\Projects...." I still had the same problem and then copied all folder in "D:\Test" folder (of course in this folder I have one more folder: tessdata)
It worked!!!
Hope it helps for you or anyone searching for this problem like me :)
Nes
If you put your code inside a Try/Catch block, you should be able to find out what the error is without your program closing. You could also debug the program instead of running it, and instead of the program crashing, the debugger will show you exactly where the error is happening.
The first parameter of Init method specifies the location of tessdata folder. If you have it at the default location, which is the same as that of Tesseract binary, it should be null, or Nothing in VB.NET.