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
Related
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
I have button to create a text file and a textbox to write stuff to the text file when I press enter.
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
path = "C:\Testing.txt"
File.Create(path)
End Sub
If e.KeyCode = Keys.Enter Then
System.IO.File.AppendAllText(path, TextBox1.Text & vbCrLf)
End If
The file is created properly but when I want to write to it using the code above, I get an error.
The process cannot access the file 'C:\Testing.txt' because it is being used by another process.
Change this line:
File.Create(path)
To this:
File.Create(path).Dispose()
However as stated before, you can cut all this out and simply use:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
path = "C:\Testing.txt"
If e.KeyCode = Keys.Enter Then
System.IO.File.AppendAllText(path, TextBox1.Text & vbCrLf)
End If
End Sub
As per MSDN: AppendAllText will "Opens a file, appends the specified string to the file, and then closes the file. If the file does not exist, this method creates a file, writes the specified string to the file, then closes the file."
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
I have the following code that simply opens up a folder with txt files.
Private Sub OpenTabpageTextFolderToolStripMenuItem_Click( _
ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles OpenTabpageTextFolderToolStripMenuItem.Click
Dim OpenFolder = (RootDrive & "QuickEmailer2\TabTxt")
Process.Start("explorer.exe", OpenFolder)
End Sub
The user then edits a txt file, and closes.
I would like to call my refresh code and make use of the changes to the txt file, but if I put the call after process.start, it runs without waiting?
I could use code to do these edit, but there are 80 files to choose from and they only need edit them once (or twice) when setting up the program for the first time.
I am sure a bit of code that says:
Private Sub OpenTabpageTextFolderToolStripMenuItem_Click( _
ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles OpenTabpageTextFolderToolStripMenuItem.Click
Dim OpenFolder = (RootDrive & "QuickEmailer2\TabTxt")
Process.Start("explorer.exe", OpenFolder)
'**I will hang on here while you do your stuff, then I will continue...**
Call RefreshfromAllTxtFiles()
End Sub
alternative solution: use 2 buttons/steps to setting up the program for the first time!
button/step #1: open setup files
button/step #2: setup files modified... PROCEED!
Along the lines of Steve's comment, you can use a FileSystemWatcher to monitor changes to the directory. Something like this can get you started:
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim fsw As FileSystemWatcher
fsw = New System.IO.FileSystemWatcher()
'this is the folder we want to monitor
fsw.Path = "c:\temp"
fsw.NotifyFilter = IO.NotifyFilters.Attributes
AddHandler fsw.Changed, AddressOf IveBeenChanged
fsw.EnableRaisingEvents = True
End Sub
Private Sub IveBeenChanged(ByVal source As Object, ByVal e As System.IO.FileSystemEventArgs)
If e.ChangeType = IO.WatcherChangeTypes.Changed Then
'this displays the file that changed after it is saved
MessageBox.Show("File " & e.FullPath & " has been modified")
' you can call RefreshfromAllTxtFiles() here
End If
End Sub
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