Open multiple file types in a VB.NET openFileDialogBox - vb.net

I have used the filter method of openFileDialogBox to allow multiple file types to be opened, but the user must select which file type they want using the dropdown. Only files with extensions matching the selected file type are visible.
How can I show all files with extensions that match any of my allowed file types? I don't want all files to be visible/openable, just the ones that I have designated.

Just specify what you want to allow... Just an example...
Dim dlg As New OpenFileDialog()
'Filter by Office Files
dlg.Filter = "Office Files|*.doc;*.xls;*.ppt"
dlg.ShowDialog()

A small snippet of how it works
Dim result As DialogResult = New DialogResult()
OpenFileDialog1.Filter = "Word Documents|*.doc;*.docx|PowerPoint Slides|*.ppt|Video Files|*.MP4|PDF Files|*.pdf"
OpenFileDialog1.InitialDirectory = "C:\"
OpenFileDialog1.FileName = ""
result = OpenFileDialog1.ShowDialog()

Related

get full path using openfiledialog

Simple utility to get a file for use.
Using openfiledialog I am trying to get the full path EG: File = text1.txt and it is located in c:\temp. So the full path is C:\temp\text1.txt.
But all I can get is the file name. I've searched I've hunted, I'e tried for a couple of hours and nothing works.
Here is code with comments...
'open the openfile dialog so the user can search for a file
Dim openFileDialog1 As New OpenFileDialog()
'set the root to the z drive
openFileDialog1.InitialDirectory = "Z:\"
'make sure the root goes back to where the user started
openFileDialog1.RestoreDirectory = True
'show the dialog
openFileDialog1.ShowDialog()
'check there is something to work with... the user did not exit before selecting a file etc.
If openFileDialog1.FileName.Length = 0 Then
'if the user selected a file set the value of the replacefile text box
Else
TB_ReplacementFile.Text = System.IO.Path.GetFullPath(openFileDialog1.FileName)
End If
All I get is the file name...
The MSDN documentation and numerous posts all over the place say all you need is openfiledialog.FileName. However this did not work for me, can't tell you why. What DID work is to use this:
TB_ReplacementFile.Text = System.IO.Path.GetFullPath(openFileDialog1.FileName)
This works great, I get what I need. I cannot explain why I have to do this. Not sure how I can be the problem, but that must be the problem right?!
Hopefully this helps someone.
The FileName Property returns the full path.
The file name includes both the file path and the extension. If no files are selected, this method returns an empty string ("").
If (openFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK) Then
TB_ReplacementFile.Text = openFileDialog1.FileName
End If
Not 100% sure if this would resolve the issue you're having, or if it's simply just another way to handle it, but I prefer to check the DialogResult. I.E:
Dim openFileDialog1 As New OpenFileDialog()
openFileDialog1.InitialDirectory = "Z:\"
openFileDialog1.RestoreDirectory = True
If openFileDialog1.ShowDialog = System.Windows.Forms.DialogResult.Ok Then
Console.WriteLine(openFileDialog1.fileName)
End If
this worked to get the directory or path
dim sDir as String = System.IO.Path.GetDirectoryName(openfiledialog1.FileName.ToString)

Editing Batch through VB.net

I am currently building a transcompiler for batch and I want an option where you can edit existing batch files through a Rich Textbox in VB.Net How does one do this? It is known knowledge that you can edit batch files through notepad without even touching the file type.
Use a StreamReader to load the text into the RichTextBox. For example:
Dim StreamReader1 As New IO.StreamReader
RichTextBox1.Text = StreamReader1.ReadToEnd()
You may also want to use an OpenFileDialog to load the file. Add a OpenFileDialog to your Form first.
OpenFileDialog1.InitialDirectory = "C:\"
OpenFileDialog1.FileName = "Open a Batch File"
OpenFileDialog1.Filter = "Batch files (*.bat) | *.bat"
OpenFileDialog1.ShowDialog()
If OpenFileDialog1.ShowDialog = DialogResult.OK Then
Dim StreamReader1 As New IO.StreamReader(OpenFileDialog1.FileName)
RichTextBox1.Text = StreamReader1.ReadToEnd
StreamReader1.Close()
Else
'What to do if OpenFileDialog is cancelled
End If

How to make textfile to save in program directory in Visual Studio

As i have abandoned the array approach to the problem, i need to know how to make listbox to save in textfile always in program's directory so it can be used/accessed to populate a different listbox, any ideas? Below is my code.
SaveFileDialog1.Filter = "Text files (.txt)|.txt"
SaveFileDialog1.ShowDialog()
If SaveFileDialog1.FileName <> "" Then
Using SW As New IO.StreamWriter(SaveFileDialog1.FileName, False)
For Each itm As String In Me.ListBox1.Items
SW.WriteLine(itm)
Next
End Using
End If
A little bit of research on your part would've helped you understand what you are trying to accomplish better.
How do I get Program Data directory? My.Computer.FileSystem.SpecialDirectories.AllUsersApplicationData
How do I Write multiple lines to file? File.WriteAllLines()
How do I Read multiple lines from a file? File.ReadAllLines()
Once you understand the basics you can easily put them together
Create two List boxes, and one button on your WinForm:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.
'Get the Program Data Directory (This is hidden by default by the OS.)
Dim strPath As String = My.Computer.FileSystem.SpecialDirectories.AllUsersApplicationData
Dim fileName As String = "myFile.txt"
Dim fullPath = Path.Combine(strPath, fileName)
Dim data As String() = {"Item 1", "Item 2", "Item 3", "Item 4", "Item 5"}
'Save the items to ListBox1 First
For Each item As String In data
ListBox1.Items.Add(item)
Next
'Now write the items to the textfile, line by line.
File.WriteAllLines(fullPath, data)
'Read all lines we just saved and load them onto an array of strings.
Dim tempAllLines() As String = File.ReadAllLines(fullPath)
'Display each on ListBox2 by iterating the array.
For Each line As String In tempAllLines
ListBox2.Items.Add(line)
Next
End Sub
Here, I created this form so you can get an idea of what i'm referring to.
You can get the path to the current executable's folder like this:
folderPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)
However, that will only work if the executable is a .NET assembly. Otherwise, you could use the first argument in the command line (which is the full executable file path), like this:
folderPath = Path.GetDirectoryName(Environment.GetCommandLineArgs()(0))
If, on the other hand, you want to get the path of the current assembly (which may be different than the executable that loaded it) you could do this:
folderPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
Or, if you want to just get the current directory, you could use this:
folderPath = Directory.GetCurrentDirectory()
Once you have the folder path, you can add the file name to it with Path.Combine, like this:
filePath = Path.Combine(folderPath, fileName)
However, it's not recommended that you write data directly to the program's running path, since the user may not have permission to write to that folder. Using the program data folder would certainly be better, but even that can be risky:
folderPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "MyAppName")
The recommended place to store data from .NET apps is Isolated Storage.

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

saving a string as a csv file?

in a winform how do i get one of those fancy windows "save file as" dialogues to appear so that the user can save a string as a file to a user-specified location on their hardrive?
Dim myString = "Hello world!"
Dim saveFileDialog As New SaveFileDialog()
saveFileDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
saveFileDialog.FilterIndex = 2
saveFileDialog.RestoreDirectory = True
If saveFileDialog.ShowDialog() = DialogResult.OK Then
If saveFileDialog.FileName <> "" Then
System.IO.File.WriteAllText(saveFileDialog.FileName, myString)
End If
End If
There's not a whole lot to it. Specify the file types to save as (in a fairly arcane format; review the docs for the Filter property to learn more), then show the dialog, and either a) retrieve the stream to write to (as shown here) with the OpenFile method, or retrieve the filename selected with the FileName property.
Review the docs on MSDN for more.
Here's an example from my project. I start the SFD in the user's my documents folder because they have guaranteed write access there.
SaveFileDialog sfd = new SaveFileDialog();
sfd.FileName = suggestedName + ".csv";
sfd.Title = "Choose Location For CSV";
sfd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
if (sfd.ShowDialog() != DialogResult.OK)
{
return;
}
string outputFileName = sfd.FileName;
oh ok, then i should rephrase my question. at runtime i am creating a string and i want to save this string as a file. how do i do this?
Dim theStringToSave as String = "some string here"
Dim sfd As New SaveFileDialog()
sfd.Filter = "txt files (*.txt)|*.txt|(*.csv)|*.csv|All files (*.*)|*.*"
sfd.FilterIndex = 2
saveFileDialog1.RestoreDirectory = True
If saveFileDialog1.ShowDialog() = DialogResult.OK Then
File.WriteAllText(sfd.FileName, theStringToSave)
End If
Search for the SaveFileDialog.