Editing Batch through VB.net - 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

Related

What is the proper way to save an existing file to a user's computer using a SaveFileDialog?

I have a vb.net windows app that creates a PDF. After creation, I want to prompt the user where they want to save the file. The default save folder is different than the folder of the created PDF. I get the SaveDialog box to come up with the default folder and file name that I want. If I choose "Save", I get a message saying that the file does not exist and none of the code below the ShowDialog is executed (I'm sure that I'm doing that part wrong as well).
Dim saveFileDialog1 As New SaveFileDialog
saveFileDialog1.InitialDirectory = MyDocsFolder
saveFileDialog1.FileName = "Report.pdf"
saveFileDialog1.Title = "Save Report"
saveFileDialog1.CheckFileExists = True
saveFileDialog1.CheckPathExists = True
saveFileDialog1.DefaultExt = "pdf"
saveFileDialog1.Filter = "All files (*.*)|*.*|All files (*.*)|*.*"
saveFileDialog1.FilterIndex = 2
saveFileDialog1.RestoreDirectory = True
saveFileDialog1.ShowDialog()
If saveFileDialog1.ShowDialog = DialogResult.OK Then
If saveFileDialog1.FileName() <> "" Then
Dim newStream As FileStream = File.Open(newFile, FileMode.Open)
Dim pdfStream As New FileStream(saveFileDialog1.FileName, FileMode.Create)
newStream.CopyTo(fs, FileMode.Append)
newStream.Close()
fs.Close()
End If
End If
you can do it like this:
Imports Microsoft.WindowsAPICodePack.Dialogs
Public NotInheritable Class Form1
Private Sub ButtonSave_Click(sender As Object, e As EventArgs) Handles ButtonSave.Click
Dim Path As String
Using SFD1 As New CommonSaveFileDialog
SFD1.Title = "Where should the file be saved?"
SFD1.Filters.Add(New CommonFileDialogFilter("PDF", ".pdf"))
SFD1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
If SFD1.ShowDialog() = CommonFileDialogResult.Ok Then
Path = SFD1.FileName
Else
Return
End If
End Using
Dim dest As IO.FileInfo
Using fs As IO.FileStream = IO.File.Create(Path & ".pdf")
dest = My.Computer.FileSystem.GetFileInfo(fs.Name)
End Using
End Sub
End Class
Please note that I am using a FileDialog that I downloaded from Visual Studios' own Nuget Package Manager. See image. You don't have to do that, but I prefer this FileDialog because it offers more options than the one that is already included.
So the user enters the file name; thus the path results. In my code example, this creates a blank PDF. It cannot be opened like this yet.
So that something is written in the PDF, you can download itext7 (also via NuGet).
Then, you write Imports iText.Kernel.Pdf,
Imports iText.Kernel.Utils and in your sub Dim pdfwriter As New PdfWriter(dest) with ‘dest’ from above.
I am so blind and stupid...
saveFileDialog1.CheckFileExists = True
Sorry for wasting anyone's time.

Save File Dialog says file is being used by another process Visual Basic

I'm making a program that opens, edits, and saves text files. So far, the opening and editing parts are working (but there's a bug with the editing part, I'll get to that later, worst case is I'll have to change the form some but I'm sure I can fix it). My main problem is the save part. Honestly, I don't know what I'm doing when it comes to Open/Save File Dialog, so I really have no idea where to begin fixing it.
I go to save a file, and I save it as a .txt file with any name (new file or overwrite it doesn't matter) and then it gives me an exception that says "The file is being used by another process" even if it's a brand new file. Here's the source:
Dim myStream As Stream
Dim saveFileDialog1 As New SaveFileDialog()
saveFileDialog1.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"
saveFileDialog1.FilterIndex = 2
saveFileDialog1.DefaultExt = ".txt"
saveFileDialog1.FileName = "New File.txt"
If saveFileDialog1.ShowDialog() = DialogResult.OK Then
myStream = saveFileDialog1.OpenFile()
If (myStream IsNot Nothing) Then
My.Computer.FileSystem.WriteAllText(saveFileDialog1.FileName.ToString, txtOutput.Text, True)
End If
End If

How to open a game executable from a **dialog** using vb.net

Can someone please tell me how to open an executable with my vb.net project. For now I have the code for the user selecting the executable from a dialog:
Dim openFileDialog1 As New OpenFileDialog()
openFileDialog1.Filter = "Executable Files|*.exe"
openFileDialog1.Title = "Select an Executable File"
If openFileDialog1.ShowDialog() = DialogResult.OK Then
Me.Cursor = New Cursor(openFileDialog1.OpenFile())
End If
You can use Icon.ExtractAssociatedIcon to extract an icon from an executable. You can then use its ToBitmap method to convert it into a bitmap that you can either assign to your PictureBox' Image property, or draw it using a graphics context.

Open multiple file types in a VB.NET openFileDialogBox

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

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