creating then writing to a text file - vb.net

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."

Related

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

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

VB Console application to rename a file

I am trying to write a Console Application in VB that will allow me to change the name of a file.
The code I have so far is:
Public Class Form1
Private Sub btnRename_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRename.Click
If txtpath.Text.Length <> 0 And txtName.Text.Length <> 0 Then
' Change "c:\test.txt" to the path and filename for the file that
' you want to rename.
' txtpath contains the full path for the file
' txtName contains the new name
My.Computer.FileSystem.RenameFile(txtpath.ToString, txtName.ToString)
Else
MessageBox.Show("Please Fill all Fields", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning)
End If
End Sub
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
txtpath.Clear()
txtName.Clear()
End Sub
End Class
But when I try to run it i get an error in this line:
My.Computer.FileSystem.RenameFile(txtpath.ToString, txtName.ToString)
Any suggestions?
Changing :
My.Computer.FileSystem.RenameFile(txtpath.ToString, txtName.ToString)
To:
My.Computer.FileSystem.RenameFile(txtpath.Text.ToString, txtName.Text.ToString)
Solves the Problem.
The problem is that you are performing .ToString on the textbox object, and not the value of the textbox. I always check to make sure that the source and destination files exist or not. Also, make sure that you are passing the full path to the files to that function to ensure it performs properly.
Try something like this:
If Not System.IO.File.Exists(txtpath.Text) Then
MsgBox("File not found!")
ElseIf System.IO.File.Exists(txtName.Text) Then
MsgBox("Target path already exists!")
Else
My.Computer.FileSystem.RenameFile(txtpath.Text, txtName.Text)
End If

How can I do something after Process.Start("explorer.exe", OpenFolder)

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

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