How to download a file and move it VB - vb.net

Okay, so I am trying to download this file https://s3.amazonaws.com/Minecraft.Download/versions/1.6.4/minecraft_server.1.6.4.exe
and put it here C:\ServerMaker\Vanilla
But when I press the button it just does nothing.
Here is the code it is mucking up with :
Private Sub Button3_Click_2(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
My.Computer.Network.DownloadFile("https://s3.amazonaws.com/Minecraft.Download/versions/1.6.4/minecraft_ server.1.6.4.exe", "C:\ServerMaker\Vanilla")
End Sub

You have spaces in the name of the file you are trying to download, remove the spaces :
My.Computer.Network.DownloadFile("https://s3.amazonaws.com/Minecraft.Download/versions/1.6.4/minecraft_server.1.6.4.exe", "C:\ServerMaker\Vanilla\minecraft_server.1.6.4.exe")
Note that the destination filename must be included .

Dim Client as new Webclient()
Client.DownloadFileAsync(New Uri("http//:s3.amazonaws.com/Minecraft.Download/versions/1.6.4/minecraft_server.1.6.4.exe"), "some path on hard drive")
And you can monitor the download progress by adding handler to do this.

Related

Download, get the filename and execute the file

I am posting here to ask you for help, thank you first of all for reading my message and being able to help me. :)
So here's the problem, I have a little program that I'm creating, in this one, I integrated a button that normally allows to download a tool to run it.
However the link of the file does not point directly in .exe it is a link like this one :
mydomain.com/file/file48.php
The problem is that I manage to get the file, but this one is named file48.php and gets the extension of a php file, what I would like is to get the original name of the downloaded .exe file in order to be able to execute it within my application.
Here is the current code I'm using :
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim client As New WebClient
AddHandler client.DownloadFileCompleted, AddressOf client_DownloadFileCompleted
client.DownloadFileAsync(New Uri("https ://mydomain.com/files/index.php"), fileName, filename)
End Sub
Private Sub client_DownloadFileCompleted(sender As Object, e As AsyncCompletedEventArgs)
Dim client = DirectCast(sender, WebClient)
client.Dispose()
Dim filename As String = CType(e.UserState, String)
End Sub
Thanks a lot for your help, I've been searching for hours without much success...

vb.net ~ How to add folder names from a directory to a list box

I'm trying to add folder names from within a directpory specified by the user to a list box. I have tried a few solutions, but can't seem to add any items. Most recently I tried:
For Each folder As String In System.IO.Path.GetDirectoryName("D:\")
ListBox1.Items.Add(folder)
Next
The form was built using VB in VB Studio Express 2013. There were no errors when running the program.
If anyone can point me in the right direction, then please help!
If you want to have a list of Directories you need to call Directory.GetDirectories(path), not Path.GetDirectoryName(path) that, in your case returns just null (passing the root directory of a drive)
For Each folder As String In System.IO.Directory.GetDirectories("D:\")
ListBox1.Items.Add(folder)
Next
if you want to show only the folder name and not the full path, just use
For Each folder As String In System.IO.Directory.GetDirectories("D:\")
ListBox1.Items.Add(Path.GetFileName(folder))
Next
Yes, I know, it seems wrong to ask for GetFileName over a folder name, but passing a full path to GetFileName returns just the folder without the path.
how to select folder name to preview picture
Imports System.IO
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For Each folder As String In System.IO.Directory.GetDirectories("C:\Program Files (x86)\KONAMI\Pro Evolution Soccer 2016\SFXPath\SweetFX")
ListBox1.Items.Add(Path.GetFileName(folder))
Next
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
If ListBox1.SelectedItem = "folder" Then PictureBox1.ImageLocation = "C:\Program Files (x86)\KONAMI\Pro Evolution Soccer 2016\SFXPath\SweetFX\HD Natural by pimplo\preview.jpg"
End Sub
End Class

Streamwriter will not save to FileName entered by User with Default var.FileName initiated - VB.NET

I am new here, and relatively new to VB.NET. I have a specific problem with getting my StreamWriter to work properly. One of the requirements of my project is to give the file a default name when the user clicks the Save button, which I have done by setting mysave.Filename = "MyLog.log". I call a new instance of my streamwriter trying to save to the filename specified by the user (mySave.Filename again), but every time, it saves to the default MyLog.log file. I have pasted my code below.
If someone could tell me how I can make sure the data is being saved to the value entered by the user for File name, that would be greatly beneficial. Also, I apologize for the code format, its not perfectly readable, but I'm trying to learn how to use the 4 space indents to my advantage!!
Thanks!
Imports System.IO
Public Class Form1
Dim Initial As String = "C:\Users\Brian Frick\Documents\Visual Studio 2010\Projects\HW5_Frick_Creator\HW5_Frick_Creator\bin\Debug" 'give variable for full path without log file
Dim Fullpath As String = "C:\Users\Brian Frick\Documents\Visual Studio 2010\Projects\HW5_Frick_Creator\HW5_Frick_Creator\bin\Debug\MyLog.log" 'give one path for full path
Dim filewriter As New StreamWriter(Fullpath, True)
Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
Application.Exit() ' quit application
filewriter.Close()
End Sub
Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
Dim mySave As New SaveFileDialog
mySave.Filter = "LOG File (*.log)|*.log" 'set filter to .log
mySave.DefaultExt = "log" 'set default extension
mySave.InitialDirectory = Initial 'set default directory
mySave.FileName = "MyLog.log"
mySave.OverwritePrompt = True ' make sure to ask user if they want to over write
If mySave.ShowDialog() = DialogResult.OK Then
Dim filewriter As New StreamWriter(mySave.FileName, True)
filewriter.Close()
'filewriter = New StreamWriter(mySave.FileName, True)
'filewriter.Close() 'close filewriter to allow access to write directory
'Dim Stream As New StreamWriter(mySave.FileName, True) 'save file to path chosen by user in SaveDialog
'Stream.Close() ' Close stream to allow access to directory
' filewriter.Close()
Else
'dialog cancelled - no action
End If
filewriter.Close()
filewriter = New StreamWriter(Fullpath, True) 're initiate filewriter to be used in successive iterations through the program without error
End Sub
Private Sub SavingsDepositBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SavingsDepositBtn.Click
'write to file for SavDep
filewriter.WriteLine("SavDep")
filewriter.WriteLine(AccountBox.Text)
filewriter.WriteLine(AmountBox.Text)
End Sub
Private Sub SavingsWithdrawBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SavingsWithdrawBtn.Click
'write to file for SavWith
filewriter.WriteLine("SavWith")
filewriter.WriteLine(AccountBox.Text)
filewriter.WriteLine(AmountBox.Text)
End Sub
Private Sub CheckDepsotBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckDepsotBtn.Click
'write to file for CheckDep
filewriter.WriteLine("CheckDep")
filewriter.WriteLine(AccountBox.Text)
filewriter.WriteLine(AmountBox.Text)
End Sub
Private Sub CheckWithdrawBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckWithdrawBtn.Click
'write to file for CheckWith
filewriter.WriteLine("CheckWith")
filewriter.WriteLine(AccountBox.Text)
filewriter.WriteLine(AmountBox.Text)
End Sub
End Class
the general way you are using the filewriter won't work, and there are some other serious issues like that there are two 'FileWriter' variables with overlapping scope.
for your save routine, you can't just re-instantiate your filewriter to point it to a different path. that will destroy all the data that had been in the underlying stream, so you are just saving nothing.
try caching the data in memory while the program is running, and don't create the stream until you are ready to write all the data to it, and it to a file, all in one go. your current approach makes it very hard to prevent leaking handles to the log files, so if the app crashes the user will have to reboot to release the write lock before they can run it again. look into the 'using' construct and 'Try... Finally'

How to save files in vb.net

I'm trying to create an app launcher in vb.net but I do not know how to save files. Save files like the one that is executed when you run a setup for an application wherein the setup will save the application files on program files folder. I'm not trying to create a vb.net setup, because I want to run my program as portable. What I want the program to do is to place the files in their appropriate location when the user clicks a button
Here's my current code:
Public Class Nircmd
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'shutdown
System.Diagnostics.Process.Start("E:\Documents and Settings\Rew\Desktop\Shutdown.lnk")
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'monitor off
System.Diagnostics.Process.Start("E:\Documents and Settings\Rew\Desktop\Monitor Off.lnk")
End Sub
End Class
-Of course it won't work if the path doesn't contain the file specified. So I want to place another button that would do just that(to save the files specified in the desired folder.
A simple syntax will do. Please
I don't quite understand, but I'll take a shot.
This will check to see if C:\foo\somefile.txt exists, and if it doesn't, create it and write some text:
If Not System.IO.File.Exists("C:\foo\somefile.txt") = True Then
Dim file As System.IO.FileStream
file = System.IO.File.Create("C:\foo\somefile.txt")
file.Close()
End If
My.Computer.FileSystem.WriteAllText("C:\foo\somefile.txt", "Some text")
If you want to copy or move a file, I think you'll want something like:
System.IO.File.Copy("C:\foo\somefile.txt", "C:\bar\somefile.txt")
or
System.IO.File.Move("C:\foo\somefile.txt", "C:\bar\somefile.txt")
I always use app.path in order to make it portable. Not everyone's computer will assign the same drive letter.

What is the file path, as a string, of a file in the application's resources?

The reason I ask is that I want to print, at run-time, a file in the application's resources, like this:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim printProcess As New Process
printProcess.StartInfo.CreateNoWindow = True
printProcess.StartInfo.FileName = "C:\Users\Geoffrey van Wyk\Documents\Countdown_Timer_Help.rtf"
printProcess.StartInfo.FileName = My.Resources.Countdown_Timer_Help
printProcess.StartInfo.Verb = "Print"
printProcess.Start()
End Sub
When I use "C:\Users\Geoffrey van Wyk\Documents\Countdown_Timer_Help.rtf" as the argument of FileName, it works. But when I use My.Resources.Countdown_Timer_Help, it says it cannot find the file.
No you didn't get it, that file will only be present on your dev machine. After you deploy your program, the file will be embedded in your program and cannot be printed. You'll have to write the code that saves the file from the resource to disk, then prints it. For example:
Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
Dim path As String = Application.UserAppDataPath
path = System.IO.Path.Combine(path, "Help.rtf")
System.IO.File.WriteAllText(path, My.Resources.Countdown_Timer_Help)
Dim printProcess As New Process
printProcess.StartInfo.CreateNoWindow = True
printProcess.StartInfo.FileName = path
printProcess.StartInfo.Verb = "Print"
printProcess.Start()
End Sub
Given that you have to save the resource to a file, it probably makes more sense to simply deploy the file with your app instead of embedding it as a resource and writing it to disk over and over again. Use Application.ExecutablePath to locate the file. To make that work at debug time you have to copy the file to bin\Debug. Do so by adding the file to your project with Project + Add Existing Item and setting the Copy to Output Directory property to Copy if Newer.
OK, I got it.
System.IO.Path.GetFullPath(Application.StartupPath & "\..\..\Resources\") & "Countdown_Timer_Help.rtf"