VB.Net code to unzip password protected file - vb.net

Below code I am using for unzip a zip file from one folder to another. But I need code two unzip password protected file. Also I need code to unzip multiple zip file from a folder at a time (One by one using loop).
Dim startPath As String = "E:\Asp.net\CC_Folders"
Dim zipPath As String = "E:\Asp.net\CC_Folder\Sci-hub.rar"
Dim extractPath As String = "E:\Asp.net\CC_Extract"
'ZipFile.CreateFromDirectory(startPath, zipPath)
'ZipFile.pass
ZipFile.ExtractToDirectory(zipPath, extractPath)

I use Ionic.Zip
Extract , add etc
https://documentation.help/DotNetZip/VB.htm
Private Sub Button6_Click(sender As System.Object, e As System.EventArgs) Handles Button6.Click
Using zip As New ZipFile()
zip.Encryption = EncryptionAlgorithm.WinZipAes256
zip.Password = "123456789"
zip.AddFile("H:Test\Floris.txt")
zip.Save("H:Test\EncryptedArchive.dll")
End Using
End Sub
Private Sub Button7_Click(sender As System.Object, e As System.EventArgs) Handles Button7.Click
' Extract a zip archive that uses AES Encryption.
' You do not need to specify the algorithm during extraction.
Using zip As ZipFile = ZipFile.Read("H:Test\EncryptedArchive.dll")
zip.Password = "123456789"
zip.ExtractAll("H:Test22")
End Using
End Sub
Private Sub Button9_Click(sender As System.Object, e As System.EventArgs) Handles Button9.Click
Try
Using zip As New ZipFile()
zip.Encryption = EncryptionAlgorithm.WinZipAes256
zip.Password = "123456789"
zip.AddDirectory("H:\Test")
zip.Save("H:\Test\Encrypt.zip")
MsgBox("Files zipped")
End Using
Catch ex As ZipException
MsgBox(ex.Message)
End Try
End Sub
Private Sub Button8_Click(sender As System.Object, e As System.EventArgs) Handles Button8.Click
Try
Using zip As ZipFile = ZipFile.Read("H:\Test\Encrypt.zip")
zip.Password = "123456789"
zip.ExtractAll("H:Test2")
MsgBox("Files unzipped")
End Using
Catch ex As Exception
End Try
End Sub
End Class

Related

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

How to set download location with FolderBrowserDialog

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

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

writing content of listbox to a txt file and then opening it

I am trying to save the contents of one listbox into a txt file and then open that up and printing it to a second listbox. i have done most of this, but my actual file is not a txt file. Can u show me how i make the file to be a txt file using my code? if this is not possible can u show me a code that can?
Private Sub LoadButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoadButton.Click
If OpenFD.ShowDialog() = DialogResult.OK Then
Dim lines = File.ReadAllLines(OpenFD.FileName)
ListBox2.Items.Clear()
ListBox2.Items.AddRange(lines)
End If
End Sub
Private Sub SaveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveButton.Click
If SaveFD.ShowDialog() = DialogResult.OK Then
Using Writer = New StreamWriter(SaveFD.FileName)
For Each o As Object In ListBox1.Items
Writer.WriteLine(o)
Next
End Using
End If
End Sub
You can just use the File.WriteAllText method (along with a string builder). My opinion, it's cleaner code.
Private Sub SaveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveButton.Click
If SaveFD.ShowDialog() = DialogResult.OK Then
Dim sb As New System.Text.StringBuilder()
For Each o As Object In ListBox1.Items
sb.AppendLine(o)
Next
System.IO.File.WriteAllText("c:\mypath\output.txt", sb.ToString())
End If
End Sub
System.IO.File.WriteAllText(Application.StartupPath & "\output.txt", sb.ToString())

Using open file dialog and save file dialog with a list box in VB

I need to save things someone adds to a list and open a txt file putting it into a list box. When I open a txt file I only get one line of code and my attempts to save only produce empty txt files. Any help will be greatly appreciated. Here is my code:
Imports System.IO
Public Class Form1
Public Listed As String
Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
Dim AllText As String = "", LineOfText As String = ""
Dim StreamToDisplay As StreamReader
OpenFileDialog1.Filter = "Text files (*.txt)}|*.txt"
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
Try
StreamToDisplay = My.Computer.FileSystem.OpenTextFileReader(OpenFileDialog1.FileName)
Label1.Text = OpenFileDialog1.FileName
Do Until StreamToDisplay.EndOfStream
LineOfText = StreamToDisplay.ReadLine()
'AllText = AllText & LineOfText & vbCrLf
lstBox.Items.Add(Listed)
Loop
lstBox.Items.Add(AllText).ToString()
StreamToDisplay.Close()
CloseToolStripMenuItem.Enabled = True
OpenToolStripMenuItem.Enabled = False
Catch ex As Exception
MsgBox("An error occurred." & vbCrLf & ex.Message)
End Try
End If
End Sub
Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
SaveFileDialog1.Filter = "Text files (*.txt)|*.txt"
If SaveFileDialog1.ShowDialog() = DialogResult.OK Then
My.Computer.FileSystem.WriteAllText(SaveFileDialog1.FileName, lstBox.Items.ToString(), False)
End If
End Sub
Private Sub CloseToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CloseToolStripMenuItem.Click
lstBox.Items.Clear()
Label1.Text = ""
CloseToolStripMenuItem.Enabled = False
OpenToolStripMenuItem.Enabled = True
End Sub
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
Dim Prompt As String = "Enter Items To Add Here"
Listed = InputBox(Prompt)
lstBox.Items.Add(Listed).ToString()
End Sub
Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click
With lstBox
.Items.Remove(.SelectedItem)
End With
End Sub
End Class
Here's a simple example that:
adds items to a ListBox
saves them to a file
loads them from a file and populates the ListBox with them
Code:
Imports System.IO
Public Class Form1
Private Sub ButtonAddItem_Click(sender As Object, e As EventArgs) Handles ButtonAddItem.Click
ListBox1.Items.Add(DateTime.Now.Ticks)
End Sub
Private Sub ButtonSave_Click(sender As Object, e As EventArgs) Handles ButtonSave.Click
If SaveFileDialog1.ShowDialog() = DialogResult.OK Then
Using writer = New StreamWriter(SaveFileDialog1.FileName)
For Each o As Object In ListBox1.Items
writer.WriteLine(o)
Next
End Using
End If
End Sub
Private Sub ButtonLoad_Click(sender As Object, e As EventArgs) Handles ButtonLoad.Click
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
Dim lines = File.ReadAllLines(OpenFileDialog1.FileName)
ListBox1.Items.Clear()
ListBox1.Items.AddRange(lines)
End If
End Sub
End Class