writing content of listbox to a txt file and then opening it - vb.net

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())

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

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

Open a txt file when a button clicked in VB.NET

I have a log file in my project. This file is a text file (.txt). Is there a way to open this file when a button clicked without using the OpenFileDialog tool?
Note that I'm using VB.NET 2010.
Dim FILE_NAME As String = "C:\FileName.txt"
If System.IO.File.Exists(FILE_NAME) = True Then
Process.Start(FILE_NAME)
Else
MsgBox("File Does Not Exist")
End If
Here is a simple example:
Public Class OpenTextFile
Private Sub OpenTextFile_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub OpenBUT_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenBUT.Click
'OpenBUT_Click the text file
Process.Start("C:\File Name.txt")
End Sub
End Class

How do you read a file and display in listbox in VB.NET?

I was trying to display data from file in a list saved on the hard drive by clicking on a button, however I'm not sure on haw to do it properly:
Private Sub btnListRecipes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnListRecipes.Click
Dim TextLine As String
If System.IO.File.Exists(Filename) = True Then
Dim RecipeReader As New System.IO.StreamReader(Filename)
Do While RecipeReader.Peek() <> -1
TextLine = TextLine & RecipeReader.ReadLine() & vbNewLine
Loop
lstRecipes.Text = TextLine.Text
Else
MsgBox("File Does Not Exist")
End If
End Sub
I would be really grateful for assistance :D
Private Sub btnListRecipes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnListRecipes.Click
Try
lstRecipes.AddRange(File.ReadAllLines(FileName))
Catch
MsgBox("Unable to read file")
End Try
End Sub
Use:
lstRecipes.Items.Add(TextLine.Text)
More specifically before it jumps to the next item in the list, so this would go right after your assignment of TextLine.
you can do this:
Imports System
Imports System.IO
Public Class Form1
Public Shared listTXT, listOfTxt, listOfTxtFile As New List(Of String)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim path As String = "C:\myDirectory\"
Dim parentinfo As New DirectoryInfo(path)
' Store Text file name in a list
For Each txtFile As FileSystemInfo In parentinfo.GetFileSystemInfos()
listTXT.Add(txtFile.Name)
Next
' Store Path of Text file in a list
For noOfTxtFile = 0 To listTXT.Count - 1
Dim pathOfFile As String = path & listTXT(noOfTxtFile)
listOfTxtFile.Add(pathOfFile)
Dim obj As System.IO.StreamReader
obj = System.IO.File.OpenText(pathOfFile)
While Not obj.EndOfStream
listOfTxt.Add(obj.ReadLine)
End While
Next
Dim lineOfTxt As Integer
Dim txt As String
For lineOfTxt = 0 To listOfTxt.Count - 1
txt = listOfTxt(lineOfTxt)
ListBox1.Items.Add(txt)
Next
End Sub
End Class

Move Files using Kill in Visual Basic

I am making a desktop cleaner and I want the program to search For files extensions and move them into a new folder each named after the extension name. Here is what I have.
Public Class Form2
Private Sub Form_Load()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim MyFolderBrowser As New System.Windows.Forms.FolderBrowserDialog
Dim dlgResult As DialogResult = MyFolderBrowser.ShowDialog()
Me.FileReference.Text = MyFolderBrowser.SelectedPath
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.Close()
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Label1.Text = "Cleaned."
If CheckBox1.Checked = True Then
On Error Resume Next
Kill(Me.FileReference.Text("\*.txt"))
If Not Directory.Exists(FileReference.Text) Then
Directory.CreateDirectory(FileReference.Text)
End If
End If
End Sub
End Class
I want to use Kill(Me.FileReference.Text("\*.txt")) to move the files with .txt extention in the Directory which the textbox named Filereference.text contains which is extracted using MyFolderBrowser.SelectedPath.
How can I do this?
For Each foundFile As String In My.Computer.FileSystem.GetFiles( _
My.Computer.FileSystem.SpecialDirectories.MyDocuments, _
FileIO.SearchOption.SearchAllSubDirectories, "*.*")
Dim foundFileInfo As New System.IO.FileInfo(foundFile)
My.Computer.FileSystem.MoveFile(foundFile, "C:\StorageDir\" & foundFileInfo.Name)
Next