How to set download location with FolderBrowserDialog - vb.net

I am trying to use FolderBrowserDialog to select the location where a file will be downloaded. This is the code for selecting a folder.
Private Sub Button15_Click(sender As Object, e As EventArgs) Handles Button15.Click
If (FolderBrowserDialog1.ShowDialog() = DialogResult.OK) Then
TextBox1.Text = FolderBrowserDialog1.SelectedPath
End If
End Sub
Now I just need to know how to take directory that appears in the textbox and have the file get downloaded to there. Here is the code for the download.
My.Computer.Network.DownloadFile("http://download1516.mediafire.com/wtzr4h1b37zg/ptzcffq933e87c8/sword_custom.png", "C:\Users\Administrator")
What would I need to replace "C:\Users\Administrator" with?

Private Sub Button15_Click(sender As Object, e As EventArgs) Handles Button15.Click
Dim URL As String = "http://download1516.mediafire.com/wtzr4h1b37zg/ptzcffq933e87c8/sword_custom.png"
Dim SaveFile As String = "sword_custom.png"
With FolderBrowserDialog1
If .ShowDialog = DialogResult.OK Then
TextBox1.Text = .SelectedPath
My.Computer.Network.DownloadFile(URL, IO.Path.Combine(.SelectedPath, SaveFile))
End If
End With
End Sub

Related

Directory.GetFiles Method Not Working In VB

I have created software with Visual Builder that cleans up your desktop. I have used the Directory.GetFiles method to move file types into certain directories. When I first coded it worked fine although I then got an error saying System.IO.IOException: 'Cannot create a file when that file already exists. Which I am not sure how to fix as I create the directory for the files with separate buttons as seen in the code.
I am also having issues with the other buttons which may be a result of the other error. When I go to clean the shortcuts which I programmed to move .lnk files into a Shortcuts folder none of them move into that folder unless they have previously been in that folder.
Full Code
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs)
MessageBox.Show("Desktop Cleaned")
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim filePaths = IO.Directory.GetFiles("C:\Users\bj\Desktop\", "*.png")
For Each filePath In filePaths
Dim filename = IO.Path.GetFileName(filePath)
Dim newPath = IO.Path.Combine("C:\Users\bj\Desktop\Pictures", filename)
IO.File.Move(filePath, newPath)
Next filePath
Dim filePaths2 = IO.Directory.GetFiles("C:\Users\bj\Desktop\", "*.jpg")
For Each filePath2 In filePaths2
Dim filename2 = IO.Path.GetFileName(filePath2)
Dim newPath2 = IO.Path.Combine("C:\Users\bj\Desktop\Pictures", filename2)
IO.File.Move(filePath2, newPath2)
Next filePath2
MessageBox.Show("Pictures Compiled And Cleaned")
End Sub
Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
Dim filePaths3 = IO.Directory.GetFiles("C:\Users\bj\Desktop\", "*.lnk")
For Each filePath3 In filePaths3
Dim filename3 = IO.Path.GetFileName(filePath3)
Dim newPath3 = IO.Path.Combine("C:\Users\bj\Desktop\Shortcuts", filename3)
IO.File.Move(filePath3, newPath3)
Next filePath3
Dim filePaths6 = IO.Directory.GetFiles("C:\Users\bj\Desktop\", "*.url")
For Each filePath6 In filePaths6
Dim filename6 = IO.Path.GetFileName(filePath6)
Dim newPath6 = IO.Path.Combine("C:\Users\bj\Desktop\Shortcuts", filename6)
IO.File.Move(filePath6, newPath6)
Next filePath6
MessageBox.Show("Shortcuts Compiled And Cleaned")
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim filePaths4 = IO.Directory.GetFiles("C:\Users\bj\Desktop\", "*.mp4")
For Each filePath4 In filePaths4
Dim filename4 = IO.Path.GetFileName(filePath4)
Dim newPath4 = IO.Path.Combine("C:\Users\bj\Desktop\Videos", filename4)
IO.File.Move(filePath4, newPath4)
Next filePath4
Dim filePaths5 = IO.Directory.GetFiles("C:\Users\bj\Desktop\", "*.avi")
For Each filePath5 In filePaths5
Dim filename5 = IO.Path.GetFileName(filePath5)
Dim newPath5 = IO.Path.Combine("C:\Users\bj\Desktop\Videos", filename5)
IO.File.Move(filePath5, newPath5)
Next filePath5
MessageBox.Show("Videos Compiled And Cleaned")
End Sub
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
My.Computer.FileSystem.CreateDirectory(
"C:\Users\bj\Desktop\Shortcuts")
End Sub
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
My.Computer.FileSystem.CreateDirectory(
"C:\Users\bj\Desktop\Videos")
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
My.Computer.FileSystem.CreateDirectory(
"C:\Users\bj\Desktop\Pictures")
End Sub
End Class
Error Code
IO.File.Move(filePath, newPath) Returns The Error,
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim filePaths = IO.Directory.GetFiles("C:\Users\bj\Desktop\", "*.png")
For Each filePath In filePaths
Dim filename = IO.Path.GetFileName(filePath)
Dim newPath = IO.Path.Combine("C:\Users\bj\Desktop\Pictures", filename)
IO.File.Move(filePath, newPath)
Next filePath
Error Message : System.IO.IOException: 'Cannot create a file when that file already exists.
Take a look at the documentation for File.Move - the example code checks if the file exists first and deletes it
Wa can extend this logic to maybe make for a nicer experience.
If IO.File.Exists(newPath) Then
Dim dr = MessageBox.Show($"File {newPath} exists, do you want to keep both files? The recently moved file will have a number added to its name", "", MessageBoxButtons.YesNoCancel)
Select dr
Case DialogResult.Cancel
Continue 'go to next loop iteration
Case DialogResult.No
IO.File.Delete(newPath)
Case DialogResult.Yes 'keep both
'Make the path eg kitten.1.jpg, kitten.2.jpg until we find a free name
Dim x = 0
Do
x += 1
newPath = IO.Path.ChangeExtension(newPath, i & IO.Path.GetExtension(newPath))
While IO.File.Exists(newPath)
End Select
End If

get file name without files extension using drag and drop vb.net

I want to drag and drop a file on a button and store the files Name without extension to a text box. Help to get out of this problem. I am getting some errors on those codes.
Private Sub Button5_DragDrop(sender As Object, e As DragEventArgs)
_Handles Button5.DragDrop
Dim files() As String = e.Data.GetData(DataFormats.FileDrop)
Dim file_name As String = Path.GetFileName(files(0))
For Each path In files
TextBox1.Text = (path)
Next
TextBox2.Text = files(0)
End Sub
Private Sub Button5_DragEnter(sender As Object, e As DragEventArgs)
_Handles Button5.DragEnter
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
e.Effect = DragDropEffects.Copy
End If
End Sub
I feel you missed setting AllowDrop feature to true
and also there is a function to get filename without extension "GetFileNameWithoutExtension"
Check the code Below
Imports System.IO
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Button5.AllowDrop = True
End Sub
Private Sub Button5_DragDrop(sender As System.Object, e As System.Windows.Forms.DragEventArgs) Handles Button5.DragDrop
Dim files() As String = e.Data.GetData(DataFormats.FileDrop)
For Each indpath In files
TextBox1.Text = Path.GetFileNameWithoutExtension(indpath) & vbNewLine & TextBox1.Text
Next
End Sub
Private Sub Button5_DragEnter(sender As System.Object, e As System.Windows.Forms.DragEventArgs) Handles Button5.DragEnter
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
e.Effect = DragDropEffects.Copy
End If
End Sub
End Class

Create a zip File From a Single file

Create a zip File From a Single file.
I have start to build a new compression tool on vb.net and its works fine, but i have to make a directory first and insert my files into it and then compress.
How can i make a compress file from a single file by press the button5.
I have try like this
ZipFileExtensions.CreateEntryFromFile(TextBox4.Text, TextBox3.Text, CompressionLevel.Optimal)
the textbox4 is the chose file and textbox3 is name for the file
but it give me the error The value of type cannot be converted to System.IO.Compression.ZipArchive
This is my code
Imports System.IO
Imports System.IO.Compression
Public Class Form2
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ProgressBar1.Show()
Timer1.Start()
TextBox2.Text = TextBox2.Text + "\" + TextBox3.Text
' Create ZIP from "source" directory (in program folder).
ZipFile.CreateFromDirectory(TextBox1.Text,
TextBox2.Text + ".zip",
CompressionLevel.Optimal,
False)
MessageBox.Show("The Process are complete", "MediaZip", MessageBoxButtons.OK, MessageBoxIcon.Warning)
ProgressBar1.Hide()
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
End Sub
Private Sub ProgressBar1_Click(sender As Object, e As EventArgs) Handles ProgressBar1.Click
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim dialog As New FolderBrowserDialog()
dialog.RootFolder = Environment.SpecialFolder.Desktop
dialog.SelectedPath = "C:\"
dialog.Description = "Select Application Configuration Files Path"
If dialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
TextBox1.Text = dialog.SelectedPath
End If
TextBox1.Text = dialog.SelectedPath
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim dialog As New FolderBrowserDialog()
dialog.RootFolder = Environment.SpecialFolder.Desktop
dialog.SelectedPath = "C:\"
dialog.Description = "Select A Folder To Compress"
If dialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
TextBox2.Text = dialog.SelectedPath
End If
TextBox2.Text = dialog.SelectedPath
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
ProgressBar1.Increment(1 * 55)
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
Dim OpenFileDialog1 As New OpenFileDialog
OpenFileDialog1.InitialDirectory = "C:\"
OpenFileDialog1.FileName = "Select A File..."
OpenFileDialog1.Multiselect = False
If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim sName As String = OpenFileDialog1.SafeFileName
TextBox4.Text = OpenFileDialog1.FileName
End If
End Sub
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
End Sub
End Class
I have finally sort all the the errors and its working like i want.
I make a temporary directory, move the chosen file into it, then compress and send it to the Desktop , and finally delete the temporary directory.
This is the working code
Imports System.IO
Imports System.IO.Compression
Imports System
Imports System.Windows.Forms
Imports System.Net
Imports System.Diagnostics
Public Class Form4
Dim tempdir As String = "C:\MediaZip"
Private Sub Form4_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Form1.Hide()
Me.BringToFront()
IO.Directory.Delete(tempdir, True)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
ProgressBar1.Show()
Timer1.Start()
TextBox2.Text = TextBox2.Text + "\" + TextBox3.Text
' Create ZIP from "source" directory (in program folder).
ZipFile.CreateFromDirectory("C:\MediaZip",
TextBox2.Text + ".zip",
CompressionLevel.Optimal,
False)
MessageBox.Show("The Process are complete", "MediaZip", MessageBoxButtons.OK, MessageBoxIcon.Warning)
ProgressBar1.Hide()
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim dialog As New FolderBrowserDialog()
dialog.RootFolder = Environment.SpecialFolder.Desktop
dialog.SelectedPath = "C:\"
dialog.Description = "Were To Store Your File ?"
If dialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
TextBox2.Text = dialog.SelectedPath
End If
TextBox2.Text = dialog.SelectedPath
Dim logDirectoryProperties As System.IO.DirectoryInfo
My.Computer.FileSystem.CopyFile(
TextBox1.Text,
"C:\MediaZip\" + TextBox4.Text)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim OpenFileDialog1 As New OpenFileDialog
OpenFileDialog1.InitialDirectory = "C:\"
OpenFileDialog1.FileName = "File To Zip..."
OpenFileDialog1.Multiselect = True
If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim sName As String = OpenFileDialog1.SafeFileName
TextBox1.Text = OpenFileDialog1.FileName
TextBox4.Text = System.IO.Path.GetFileName(OpenFileDialog1.FileName)
IO.Directory.Delete(tempdir, True)
End If
End Sub
End Class

opening files added to a combobox

Dim dir = "..//Football/"
Private Sub FTablebutton_Click(sender As Object, e As EventArgs) Handles FTablebutton.Click
For Each file As String In System.IO.Directory.GetFiles(dir)
FfilesComboBox.Items.Add(System.IO.Path.GetFileNameWithoutExtension(file))
Next
End Sub
Private Sub FfilesComboBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles FfilesComboBox.SelectedIndexChanged
Dim openfile As String = System.IO.Path.Combine(dir, FfilesComboBox.SelectedItem.ToString)
'start the process using the openfile string
Process.Start(openfile)
End Sub
I am able to add all the files to combobox but the problem is i cannot open the file when selected from the combobox
Try this
Private Sub FTablebutton_Click(sender As Object, e As EventArgs) Handles FTablebutton.Click
For Each file As String In System.IO.Directory.GetFiles(dir)
FfilesComboBox.DisplayMember = "key"
FfilesComboBox.ValueMember = "value"
FfilesComboBox.Items.Add(New DictionaryEntry(System.IO.Path.GetFileNameWithoutExtension(file), System.IO.Path.GetFileName(file)))
Next
End Sub
Private Sub FfilesComboBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles FfilesComboBox.SelectedIndexChanged
Dim openfile As String = System.IO.Path.Combine(dir, FfilesComboBox.SelectedItem.Value.ToString)
'start the process using the openfile string
Process.Start(openfile)
End Sub
If you Then use Visual studio 2008 or newer. you can use Anonymous class to store the full file path with the FileNameWithoutExtension.
Dim dir = "..//Football/"
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
FfilesComboBox.DisplayMember = "Text"
End Sub
Private Sub FTablebutton_Click(sender As Object, e As EventArgs) Handles FTablebutton.Click
For Each file As String In System.IO.Directory.GetFiles(Dir)
FfilesComboBox.Items.Add(New With {.Text = System.IO.Path.GetFileNameWithoutExtension(file), .Value = file})
Next
End Sub
you can use ComboBox1.SelectedItem.Value to get the value (that is the file full path)
Private Sub FfilesComboBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles FfilesComboBox.SelectedIndexChanged
'start the process using the openfile string
Process.Start(FfilesComboBox.SelectedItem.Value)
End Sub
This will work even you choose to loop over sub directories.
This code is tested and worked fine

Open a ".exe" with a Button using a file path from a TextBox

I have a TextBox called TextBox1, which is filled by a Button that gets a file path using OpenFileDialog. I want a button (Button3) to start several processes one after another with an interval of 2 hours then close it and open the next one.
In total I have 4 different TextBoxes (TextBox1, TextBox2, TextBox3 and TextBox4) and 4 different file paths that I want to open with the same button with the interval I mentioned before.
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
OpenFileDialog1.Title = "Please Select a File"
OpenFileDialog1.InitialDirectory = "C:temp"
OpenFileDialog1.ShowDialog()
End Sub
Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
Dim strm As System.IO.Stream
strm = OpenFileDialog1.OpenFile()
TextBox1.Text = OpenFileDialog1.FileName.ToString()
If Not (strm Is Nothing) Then
'insert code to read the file data
strm.Close()
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
OpenFileDialog2.Title = "Please Select a File"
OpenFileDialog2.InitialDirectory = "C:temp"
OpenFileDialog2.ShowDialog()
End Sub
Private Sub OpenFileDialog2_FileOk(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog2.FileOk
Dim strm As System.IO.Stream
strm = OpenFileDialog2.OpenFile()
TextBox3.Text = OpenFileDialog2.FileName.ToString()
If Not (strm Is Nothing) Then
'insert code to read the file data
strm.Close()
End If
End Sub
Private Sub TextBox3_TextChanged(sender As Object, e As EventArgs) Handles TextBox3.TextChanged
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
End Sub
End Class
To launch a new process use:
dim myProcess = Process.start(filename)
You dont' need the code that does the following:
Dim strm As System.IO.Stream
strm = OpenFileDialog2.OpenFile()
If Not (strm Is Nothing) Then
strm.Close()
End If
This is opening the exe file as if it were trying to read the data from it.
Instead just use
dim process = Process.Start(OpenFileDialogX.Filename)
Note: your initial directory seems to be c:temp not c:\temp as it probably should be