Open a txt file when a button clicked in VB.NET - 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

Related

Open another form when clicking an item in ListBox VB.net

I'm a network admin that have been task to do a paperless meeting system. So right now I'm looking at VB.net as my platform. My goal is populate a listbox with items inside a folder using their filenames, it's mostly PDF files. So i have achieve that goal and when I click the items in the listbox it the file in the folder opens. What I want is, if I click an item in the listbox, another form will show. This form has an embedded pdf reader and a textbox for comments. textbox will be save in a .txt file inside a another folder. here's my code
Public Class Form2
Dim MyFolderPath As String = IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "C:\Users\ICTCAdmin\Desktop\Board Meeting\Academic")
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
For Each fullpath As String In IO.Directory.GetFiles(MyFolderPath)
ListBox1.Items.Add(IO.Path.GetFileName(fullpath))
Next
Catch ex As Exception
MsgBox(ErrorToString)
End Try
End Sub
Private Sub Form2_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Form1.Show()
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
Dim fullpath As String = IO.Path.Combine(MyFolderPath, ListBox1.SelectedItem.ToString)
Process.Start(fullpath)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
End Class
Thank you in advance.
In your second form create a Friend property like this at the top of the child form's code window
Public Class frmWhateverYourNewFormIsCalled
Friend Property filepath As String
In your calling form edit the listbox1_SelectedIndexChanged property to this
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
Dim fullpath As String = IO.Path.Combine(MyFolderPath, ListBox1.SelectedItem.ToString)
dim childForm as new frmWhateverYourNextFormIsCalled
childForm.filepath = ""
childForm.Show()
End Sub
In your child form add in code to open the filePath in your embedded pdf reader. Probably in the Shown event handler. Not the Load event handler.
To view pdfs within your program this might help
Displaying a PDF in a control in Visual Basic 2010

writing content of listbox to a txt file and then opening it

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

Button that opens Application Path + specific folder

I'm kind of confused on how to do this. What I want to do is when I click Button1, my program will open a folder in Explorer, and the second button will open a file as a text file.
Here's my code:
Button 1
Process.Start("explorer.exe", Application.ExecutablePath + "\mvram.biz")
Button 2
Process.Start("Notepad.Exe", "README.txt")
My problem is everytime I click the button it will open My Documents. It must open the APPpath+Specific Folder.
EDIT:
Public Class Form1
Private Sub ExcisionButtonDefault5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExcisionButtonDefault5.Click
Me.Close()
End Sub
Private Sub ExcisionButtonDefault1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExcisionButtonDefault1.Click
Dim path As String = System.IO.Path.GetDirectoryName(Application.ExecutablePath) & "\mvram.biz\"
Process.Start("explorer.exe", path)
End Sub
Private Sub ExcisionButtonDefault2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExcisionButtonDefault2.Click
Process.Start("explorer.exe", Application.StartupPath & "\Documents")
End Sub
Private Sub ExcisionButtonDefault3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExcisionButtonDefault3.Click
Process.Start("Notepad.Exe", "/select," & "README.txt")
End Sub
Private Sub ExcisionButtonDefault4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExcisionButtonDefault4.Click
Process.Start("explorer.exe", System.Windows.Forms.Application.StartupPath + "\Presentation")
End Sub
Private Sub ExcisionButtonDefault6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExcisionButtonDefault6.Click
System.Diagnostics.Process.Start("http://www.mvram.biz")
End Sub
End Class
The reason why it opens a random location is because you are intending to execute a wrong path (whole app path + other app). You have to choose the directory. Try this code:
Dim path As String = System.IO.Path.GetDirectoryName(Application.ExecutablePath) & "\mvram.biz"
Process.Start("explorer.exe", path)
Other option:
Dim path As String = Environment.CurrentDirectory & "\mvram.biz"
I personally prefer to use absolute paths rather than relative ones (just the name of the file when referring to the same directory).
To open a specific folder in FileExplorer, you can try passing this argument:
Dim x as string = "FolderPath"
Process.Start("explorer.exe", "/root,x")
Or you could pass the argument "/select,x", which will open File Explorer with the folder selected, but not opened. This article may be helpful. Or you can just have the file address, like you tried above, and it will open to the correct location.
And then to open a txt file, all you need to do is:
Process.Start("FileAddress")
This will open the file in its default editor.
HTH

vb.net write text to file ussing radio button

Hi I would like to know how i can write to the following file C:test.txt a specific string of text like "LANDSCAPE" if a radiobutton is clicked?
If you simply want to create a file containing nothing but the text "LANDSCAPE", and you want to overwrite the file by that name if it already exists, then all you need to do is:
Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
If RadioButton1.Checked Then
File.WriteAllText("C:\test.txt", "LANDSCAPE")
End If
End Sub
Add the file writing code to checked change event of the radio button.
Private Sub RadioButton1_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles RadioButton1.CheckedChanged
If RadioButton1.Checked = True Then
'Write to file
End If
End Sub

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