OpenFileDialog Filter Option Not Working - vb.net

Since I put my OpenFileDialog into a BackgroundWorker, the Filter option no longer works.
I use to have this as a button click and it worked fine, with the exception of what every file I opened it wouldn't close the file, hence I added the BackgroundWorker.
Anywho, here's my current code, nothing different from the button click code I had.
Dim OpenFileDialog2 As New OpenFileDialog()
OpenFileDialog2.InitialDirectory = "C:\Temp\Config_Files\"
OpenFileDialog2.Filter = "Configuration Files (*.cfg)|*.cfg"
Is there something I need to add to make this work properly?

I think that you are misunderstanding the advice to use a backgroundworker.
You should let the OpenFileDialog do its work and grab the cfg file to be processed, then, if you want a faster UI response start the backgroundworker.
Dim fileToProcess as String = string.Empty
Using opf As New OpenFileDialog()
opf.InitialDirectory = "C:\Temp\Config_Files\"
opf.Filter = "Configuration Files (*.cfg)|*.cfg"
if opf.ShowDialog() = DialogResult.OK then
fileToProcess = opf.FileName
Endif
End Using
if fileToProcess <> string.Empty then
' Now start you backgroundworker to do its job
end if
Of course, the user could re-start againg the same code and select again the same file. This could lead to unexpected results. Better to disable the Button/Menu or whatever that start the file selection process till the previous process ends.

Related

How can i open more than 1000 files using openfiledialog? Or is there any method i can use?

I am writing a small program in which user will select set of file (mostly .CSV) files and my program will search through them and find required data.
It's working for up to 300 files, but after that its not working. It's giving me an error:
InvalidOperationExceprion was Unhandaled
Too Many files are selected. Select Fewer files and try again.
What should i do?
Like Eric Walker said, open file dialog works with 1,000's and 1,000's of files, there is no reason why it would stop at 300.
The best way to loop though files/folders to get info is to use the .GetFiles() method
Dim OFD As New FolderBrowserDialog
If OFD.ShowDialog = DialogResult.OK Then
For Each f In Directory.GetFiles(OFD.SelectedPath)
If Path.GetExtension(f) = ".txt" Path.GetExtension(f) = ".csv" Then
Dim reader As String() = File.ReadAllLines(f)
For each line as String in reader
DoSomethingAwesome(line)
Next
End If
Next
End If
This will cycle through every file in a certain Directory.
Now if you would like to cycle through every file in a file dialog, then you would use this.
Dim OFD As New OpenFileDialog()
OFD.Multiselect = True
If OFD.ShowDialog = DialogResult.OK Then
For Each f In OFD.FileNames
If Path.GetExtension(f) = ".txt" Path.GetExtension(f) = ".csv" Then
Dim reader As String() = File.ReadAllLines(f)
For Each line As String In reader
DoSomethingAwesome(line)
Next
End If
Next
End If
Give one of these a try depending on your preference.
As a side note, for future posts - please post your attempted code or more details on what you are trying to accomplish and where you are having trouble. Frankly, Im surprised you weren't downvoted (very surprised, people on SO can be ruthless). Just a friendly tip.

Unhandled exception has occured in the application vb.net

Ok, so my code look like the following.
Dim file As System.IO.StreamWriter
file = My.Computer.FileSystem.OpenTextFileWriter("E:/Med/Dra.txt", False)
file.WriteLine(NameBasic)
file.WriteLine(LastBasic)
file.WriteLine(PhoneBasic)
file.Close();
All those are variables that I have set for text boxes. This is OnbuttonClick(...
Now for my onload I take the info out of the notepad, Here is the code,
Dim read As System.IO.StreamReader
read = My.Computer.FileSystem.OpenTextFileReader("E:/Med/Dra.txt")
lblNameBasic.Text = read.ReadLine
lblLastBasic.Text = read.ReadLine
lblPhoneBasic.Text = read.ReadLine
read.Close();
I have placed the notepad(txt file) inside a flashdrive folder named med
I got the saving info to work and load, so I took the flashdrive to another computer, I got this nasty error, talking about the System.IO and all this other stuff.
It then prompts me, would you like to continue with errors, or quit.
I click continue than all the saved data does not load. Am I doing something wrong here??
Also sorry for alot of questions today. (The .exe is in the flashdrive, med folder aswell).
First of all, your path is incorrect - E:/Med/Dra.txt should be E:\Med\Dra.txt. And here how you use open file dialog - this is just basis, you need to take care of error handling, etc.
Dim openFileDialog1 As New OpenFileDialog()
openFileDialog1.InitialDirectory = "c:\"
openFileDialog1.Filter = "txt files (*.txt)|*.txt"
openFileDialog1.RestoreDirectory = True
If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
Dim read As System.IO.StreamReader = My.Computer.FileSystem.OpenTextFileReader(openFileDialog1.FileName)
End If
I think, main reason why you had errors is because incorrect path. You can also check if path exists
If Not File.Exists("E:\Med\Dra.txt") Then
MessageBox.Show("There is no such file")
Exit Sub
End If
' Code to open non existing file will be skipped

vb.net downloading a file without popups

I am trying to download tracking information from FedEx's website. First I have to pass login credentials and then navigate to a webpage which triggers an automatic download. My code so far works fine for this. However; as soon as the download is triggered a popup comes up asking if I want to save the file and then when I click save I asks where I want to save the file to. I want to disable the popups and have the file just automatically saved in my downloads folder. Is there a way to do this? Thanks in advance for any help!
Here is my code so far.
Dim WebBrowser1 As New WebBrowser
Dim url As String = "https://www.fedex.com/insight/manifest/manifest.jsp?&__QS=252D0F3B4E380B211B122B1A09251510050E0F5C273A223E34360539237976645E45745E57776C&"
WebBrowser1.Navigate(url)
WebBrowser1.ScriptErrorsSuppressed = True
Threading.Thread.Sleep(2000)
Application.DoEvents()
Do Until WebBrowser1.IsBusy = False
Threading.Thread.Sleep(1000)
Application.DoEvents()
Loop
WebBrowser1.Document.GetElementById("username").SetAttribute("value", "MyUsername")
WebBrowser1.Document.GetElementById("password").SetAttribute("value", "MyP#ssw0rd")
WebBrowser1.Document.GetElementById("login").InvokeMember("click")
Threading.Thread.Sleep(1000)
Application.DoEvents()
Do Until WebBrowser1.IsBusy = False
Threading.Thread.Sleep(1000)
Application.DoEvents()
Loop
WebBrowser1.Navigate("https://www.fedex.com/insight/manifest/download_post.jsp?VIEW=|Outbound_View&INFOTYPE=STATUS")
Do Until WebBrowser1.IsBusy = False
Threading.Thread.Sleep(1000)
Application.DoEvents()
Loop
So I tried this code but all this does is still just download the page's source.
Using client As New WebClient()
client.Headers("User-Agent") = "Mozilla/4.0"
client.Credentials = New NetworkCredential("Username", "P#sword")
client.Credentials = CredentialCache.DefaultCredentials ' << if Windows Authentication
Dim content As String = client.DownloadString("https://www.fedex.com/insight/manifest/download.jsp?VIEW=/Outbound_View")
Console.WriteLine(content.Substring(0, 15))
End Using
I checked the webpage's source again to try to find a different url but also could not find anything else. I was wondering if it would be easier to download using WebBrowser.Navigate and then suppress the popups. Is there away to suppress or get rid of the download popups in vb? I am just not having much success with the WebClient. Thanks for all the help as I am still pretty new at vb.net!

Kill all processes in a listBox? (VB.NET)

I'm trying to create a small productivity program to keep myself focused when programming; specifically, to close any processes that might distract me from doing my job. I'm writing it in VB.NET for simplicity.
What is the easiest way to kill all processes listed in a listBox? I already know how to add the processes to my listBox with this code:
Dim newProc As New OpenFileDialog
'// Settings for the open file dialog. (I like how I use ' to start the comment, but // so I recognize it! :)
newProc.Filter = "Executable files (*.exe)|*.exe"
newProc.FileName = "..choose a file.."
newProc.Multiselect = True
newProc.CheckFileExists = True
newProc.CheckPathExists = True
newProc.AutoUpgradeEnabled = True
newProc.AddExtension = True
If (newProc.ShowDialog = Windows.Forms.DialogResult.OK) Then
ListBox1.Items.AddRange(newProc.SafeFileNames)
End If
This adds the processes to the listBox very neatly and all, exactly how I want it. I have a timer that gets enabled with the press of a button that should close all processes in the listBox, but I'm unsure what I should use. Can I get some help? :(
Dim pProcess() As Process = System.Diagnostics.Process.GetProcessesByName("notepad")
For Each p As Process In pProcess
p.Kill()
Next
You can try the above. Please view this link for further infomation.

How can I make a "browse for file.." button in VB.net?

I'm trying to build a simple FTP uploader. How can I make it so the user can select a file to upload? See, what I want is to have a button (which I do) that a user can click, and it shows the OpenFileDialog (which I have), but then when they select a file, I want its path to be shown in a text box. How can I do that?
Try the following code
Dim dialog As New OpenFileDialog()
If DialogResult.OK = dialog.ShowDialog Then
TextBox1.Text = dialog.FileName
End If
One way is to convert the filename to a FileInfo which contains all sorts of information about the file including the path. This opens the dialog and displays the path of the selected file.
If OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
Dim fi As New System.IO.FileInfo(OpenFileDialog1.FileName)
TextBox1.Text = fi.DirectoryName
End If
You want to get the OpenFileDialog's property Filename property. See OpenFileDialog's class members here on MSDN.
Hope this helps
Add a OpenFileDialog And Add This Code
If YourOpenFileDialogName.ShowDialog = YourOpenFileDialogName.OK Then
textBox1.Text = YourOpenFileDialogName.FileName
End If