adding a file into a listbox in vb.net / vb2008 - vb.net

Hello i store proxys in a notpad.txt file and im trying to grab all proxys in the notpad and put them into listbox1
i am using
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Using Ofd As New OpenFileDialog
Ofd.Filter = "All files (*.*)|*.*"
If Ofd.ShowDialog = 1 Then ListBox1.Items.AddRange(IO.File.ReadAllLines(Ofd.FileName))
End Using
End Sub
I click it the button it lets me pick a file but does not import the stuff in the file to listbox1
Please help

I tested your code and it works for me, so I assume the problem is with the format of your file. How was the file created? Could you provide a link to it so I can take a look?
One thing to note, you should use the DialogResult Enumeration instead of the magic number 1 for the OK result to improve the readability and maintenance of your code.
If Ofd.ShowDialog = DialogResult.OK Then ListBox1.Items.AddRange(IO.File.ReadAllLines(Ofd.FileName))

try this example add it if you click on the OK button
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Using Ofd As New OpenFileDialog
Ofd.Filter = "All files (*.*)|*.*"
If Ofd.ShowDialog = 1 Then
'Pass the file path and file name to the StreamReader constructor
Dim sr As New StreamReader(Ofd.FileName)
Dim line As String = String.Empty
Try
'Read the first line of text
line = sr.ReadLine()
'Continue to read until you reach end of file
While line IsNot Nothing
Me.listBox1.Items.Add(line)
'Read the next line
line = sr.ReadLine()
End While
'close the file
sr.Close()
Catch e As Exception
MessageBox.Show(e.Message.ToString())
Finally
'close the file
sr.Close()
End Try
End If
End Using
End Sub
Regards

Related

File.create shows no errors but doesnt create file?

I'm trying to copy a file from an external drive by reading its contents, creating a new file elsewhere and writing the contents into it. My code shows no errors (using MSV) but when I try to 'download' the file, it completes the code but no file is created.
Can anyone help?
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim FileReader As String
FileReader = My.Computer.FileSystem.ReadAllText(Label32.Text)
Dim fbd As FolderBrowserDialog = New FolderBrowserDialog
Dim DownloadLocation As String
If fbd.ShowDialog() <> DialogResult.Cancel Then
DownloadLocation = fbd.SelectedPath
File.Create(fbd.SelectedPath & "pandora speedsign log.txt").Dispose()
File.WriteAllText(fbd.SelectedPath & "pandora speedsign log.txt", FileReader)
MessageBox.Show("success!!")
End If
'File.Create("C:\Users\%UserProfile%\Downloads" & DownloadFileDate & ".txt")
'File.WriteAllText("C:\Users\%USERPROFILE%\Downloads" & DownloadFileDate & ".txt", FileReader)
End Sub
I've been looking for different ways of creating the file, different ways of writing the file but nothing seems to work.
We can significantly simplify the code like this:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim fbd As New FolderBrowserDialog()
If fbd.ShowDialog() <> DialogResult.Ok Then Exit Sub
Dim outputPath As String = IO.Path.Combine(fbd.SelectedPath, "pandora speedsign log.txt")
IO.File.Copy(Label32.Text, outputPath)
End Sub

How to separate readable and unreadable images into different folders?

I have the folder D:\both_img. In that folder I have a bulk of both readable and unreadable .bmp images.
How can I move the unreadable images to a another folder?
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim openfiled1 As New OpenFileDialog
If openfiled1.ShowDialog <> DialogResult.Cancel Then
PictureBox1.Image = Image.FromFile(openfiled1.FileName)
End If
End Sub
Try the following code:
Dim di As New DirectoryInfo("D:\both_img")
Dim fiArr As FileInfo() = di.GetFiles()
Dim fi As FileInfo
For Each fi In fiArr
Try
Dim image1 As Bitmap = CType(Image.FromFile(fi.FullName, True), Bitmap)
fi.MoveTo(validFiledestPath) 'Move to valid file folder
Catch ex As OutOfMemoryException
fi.MoveTo(invalidFileDestPath) 'Move to invalid file folder
End Try
Next fri
This code iterates all the files. Tries to open them using Image.FromFile and if it opens moves them to valid folder otherwise move to invalid folder.
References

How to select a specific path for a savefiledialog in visual studios

I am stuck on how to make a specific path way to a certain folder when I save a image. I have no idea where to start or where to put the pathway within the command.
'I have recent put System.windows.forms. in and found it still worked'
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim savefiledialog1 As New SaveFileDialog
Try
savefiledialog1.FileName = "*.tif"
savefiledialog1.Filter = "Tiff | *.tif"
If savefiledialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
PictureBox1.Image.Save(savefiledialog1.FileName, System.Drawing.Imaging.ImageFormat.Tiff)
End If
Catch ex As Exception
End Try
End Sub
I think this should work with following code:
savefiledialog1.InitialDirectory = "your/path"
More: MSDN

Save Data to text tile using SaveFileDialog?

I have already viewed the MSDN Example but I am still having problems.
I created a super-simple program to multiply two numbers, and display the output in the textbox. Now I need to be able to read that text box value and put the value in a text file, bringing up the save to file dialog when the "Save To File" button is clicked.
Private Sub MutiplyBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MutiplyBtn.Click
Dim FirstNum As Double = Num1.Text
Dim SecondNum As Double = Num2.Text
Dim Answer2 As Double = FirstNum * SecondNum
Answerbox.Text = Answer2
End Sub
Private Sub SaveResultToFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveResultToFile.Click
Dim myStream As Stream
Dim saveFileDialog1 As New SaveFileDialog()
saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
saveFileDialog1.FilterIndex = 2
saveFileDialog1.RestoreDirectory = True
If saveFileDialog1.ShowDialog() = DialogResult.OK Then
myStream = saveFileDialog1.OpenFile()
If (myStream IsNot Nothing) Then
System.IO.File.WriteAllText(Answerbox.Text)
myStream.Close()
End If
End If
End Sub
Currently, Visual Studio is giving me an error: Overload resolution failed because no accessible 'WriteAllText' accepts this number of arguments.
WriteAllText static method requires the name of the file where the data should be written to.
You could use directly the name selected in the saveFileDialog1
If saveFileDialog1.ShowDialog() = DialogResult.OK Then
System.IO.File.WriteAllText(saveFiledialog1.FileName, Answerbox.Text)
End If
instead if you really want to use the stream opened by OpenFile() method your code should be
If saveFileDialog1.ShowDialog() = DialogResult.OK Then
Dim sw As StreamWriter = new StreamWriter(saveFileDialog1.OpenFile())
If (sw IsNot Nothing) Then
sw.WriteLine(Answerbox.Text)
sw.Close()
End If
End If
The code is an example, you need to add a bit of error handling
Hi I tried above method but I succeed in this way....
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
SaveFileDialog1.Filter = "TXT Files (*.txt*)|*.txt"
If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK _
Then
My.Computer.FileSystem.WriteAllText _
(SaveFileDialog1.FileName, RichTextBox1.Text, True)
End If
End Sub

How to op-en file and view in a rich text box using vb.net?

Hi I am trying to open and view a files text in a rich text box. Here is what I have please let me know what I am doing wrong?
Private Sub loadButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles loadButton.Click
' Displays an OpenFileDialog so the user can select a Cursor.
Dim openFileDialog1 As New OpenFileDialog()
openFileDialog1.Filter = "Cursor Files|*.txt"
openFileDialog1.Title = "Select a Cursor File"
If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
' Assign the cursor in the Stream to the Form's Cursor property.
Me.mainRTBox = New Text(openFileDialog1.OpenFile())
End If
End Sub
The problem you were having was that you weren't reading the file at all, and you weren't assigning the content of the file to the RichTextBox correctly.
Specifically, this code you have:
Me.mainRTBox = New Text(openFileDialog1.OpenFile())
.. should be:
Me.mainRTBox.Text = FileIO.FileSystem.ReadAllText(openFileDialog1.FileName)
This code will work:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
' Displays an OpenFileDialog so the user can select a Cursor.
Dim openFileDialog1 As New OpenFileDialog()
openFileDialog1.Filter = "Cursor Files|*.cur"
openFileDialog1.Title = "Select a Cursor File"
If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
' Assign the cursor in the Stream to the Form's Cursor property.
Dim extension = openFileDialog1.FileName.Substring(openFileDialog1.FileName.LastIndexOf("."))
If extension Is "cur" Then
Me.mainRTBox.Text = FileIO.FileSystem.ReadAllText(openFileDialog1.FileName)
End If
End If
End Sub
End Class
Edit: I updated the code so that it checks if the user did actually open a Cur (cursor) file.
RichTextBoxes have a built in function for viewing RTF files and TXT files.
Code for a RTF file:
RichTextBox1.LoadFile("YOUR DIRECTORY", RichTextBoxStreamType.RichText)
Code for a TXT file:
RichTextBox1.LoadFile("YOUR DIRECTORY", RichTextBoxStreamType.PlainText)
Hope it helps
-nfell2009